VS2005 - question about copy-ctor

  • Thread starter Thread starter Patrick Kowalzick
  • Start date Start date
P

Patrick Kowalzick

Hello all,

find below an example which IMO should refuse to compile. It compiles fine
with VS2005.

What happens in the call?
What is your opinion?

Kind regards,
Patrick



class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private:
// hide the copy-ctor and assignment
noncopyable( noncopyable const & );
const noncopyable& operator=( noncopyable const & );
};


class A : public noncopyable // inherit to avoid copying
{
noncopyable copy_protection; // make member to avoid copying
};


int main()
{
//A a;
//A b = a; // this fails to compile

A c = A(); // this compiles fine

return 0;
}
 
Patrick said:
Hello all,

find below an example which IMO should refuse to compile. It compiles fine
with VS2005.

What happens in the call?
What is your opinion?

I agree that it shouldn't compile, and for 2 separate reasons:

1: A's default constructor cannot access the default constructor for the
member copy_protection - remember that protected access only gives you
access to protected members of bases through pointers, references and
objects of the derived class, not the base class (this is to prevent one
derived class from breaking objects of other derived classes derived
from the same base). As a result, A doesn't have a well-formed default
constructor, so even "A a;" shouldn't compile. This is covered in 11.5/1
of the standard.

Delete the member copy_protection to get around this, and try a
different compiler to get the error in the first place.

2: In the call, "c" is copy initialized from a default constructed A.
The compiler is allowed to optimize out the copy, but according to the
standard it must check that the copy could be made (e.g. that there is
an accessible copy constructor). This is covered in 12.2/1 of the standard.

Again, use a different compiler to get the error.
class A : public noncopyable // inherit to avoid copying
{
noncopyable copy_protection; // make member to avoid copying
};

Should be:

class A : noncopyable // no need for public inheritance
{
};
int main()
{
//A a;
//A b = a; // this fails to compile

A c = A(); // this compiles fine

return 0;
}

You may want to report the bugs to MS.

Tom
 
Hello Tom,
I agree that it shouldn't compile, and for 2 separate reasons:

1: A's default constructor cannot access the default constructor for the
member copy_protection - remember that protected access only gives you
access to protected members of bases through pointers, references and
objects of the derived class, not the base class (this is to prevent one
derived class from breaking objects of other derived classes derived from
the same base). As a result, A doesn't have a well-formed default
constructor, so even "A a;" shouldn't compile. This is covered in 11.5/1
of the standard.

Delete the member copy_protection to get around this, and try a different
compiler to get the error in the first place.

Normally, I do not use a member copy_protection. It was just to test VS2005
:).
2: In the call, "c" is copy initialized from a default constructed A. The
compiler is allowed to optimize out the copy, but according to the
standard it must check that the copy could be made (e.g. that there is an
accessible copy constructor). This is covered in 12.2/1 of the standard.

Again, use a different compiler to get the error.

VS2005 is the first, where I do not get the error. I was a little bit
surprised, as this is quite basic stuff. But perhaps MS will launch a 8.1
soon :). 8.0 is a little bit annoying sometimes.
Should be:

class A : noncopyable // no need for public inheritance
{
};
True.

You may want to report the bugs to MS.

Yes, I want. There is antoherone I want to report, but I do not know how. I
googled a little bit, but was not successful, yet.

Regards,
Patrick
 
Hello all,
You can do that here:
http://lab.msdn.microsoft.com/productfeedback/default.aspx

Then, after you did that you can post the url here so that we can validate
it and vote for it.

I posted it here:
http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackid=FDBK47765

I wanted to refer to
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=FDBK18668

but I did not manage it. I opened a new one, because the latter one refers
to the beta-version.

Regards,
Patrick
 
Patrick said:
Hello all,




I posted it here:
http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackid=FDBK47765

I wanted to refer to
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=FDBK18668

but I did not manage it. I opened a new one, because the latter one refers
to the beta-version.

Just to clarify, the following program should *not* compile - if it does
in VS2005 (which I don't have handy), it's another bug which should be
reported:

class Foo
{
protected:
Foo(){}
};

class Bar: public Foo
{
Foo f;
};

int main()
{
Bar b;
}

Tom
 
Tom said:
Just to clarify, the following program should *not* compile - if it
does in VS2005 (which I don't have handy), it's another bug which should
be
reported:

class Foo
{
protected:
Foo(){}
};

class Bar: public Foo
{
Foo f;
};

int main()
{
Bar b;
}

It does compile with VC8. Looks like another bug is needed.

-cd
 
Hello all,

MS marked the bug as resolved with the note that
A a = A();

is a direct initialization. As I wondered what the difference between this
and
A a;

shall be, I just posted in comp.lang.c++.moderated.

Regards,
Patrick
 
Patrick said:
Hello all,
Hello.



MS marked the bug as resolved with the note that
A a = A();

is a direct initialization. As I wondered what the difference between this
and
A a;

shall be, I just posted in comp.lang.c++.moderated.

A a = A();
is a copy-initialization; it is however equivalent to a
direct-initialization, since the type of the initializer is the same as
that of "a". The copy constructor is required to be accessible, but it
is implementation defined whether a temporary will be created or not.
The above is semantically equivalent to:
A a((A())); //extra parens to prevent it being a function prototype.

A a;
is a default-initilazation (assuming A is non-POD). No temporary is created.

I've posted a comment to the bug suggesting that the resolution is
incorrect.

Tom
 
I've posted a comment to the bug suggesting that the resolution is
incorrect.

I added a comment and tried with Comeau (they seem to agree with us). So I
reopened it. Hope thats not inpolite ;).

Regards,
Patrick
 
....now playing the bouncing reopen-close game :(. If they just close it
another time without argueing, I'll drop it.

No real fun with MS product feedback.

Regards
Patrick
 
Patrick said:
...now playing the bouncing reopen-close game :(. If they just close it
another time without argueing, I'll drop it.

No real fun with MS product feedback.

Looks like they can be spoilsports! You could post a link to your clc++m
thread, in particular to the post where Daveed Vandevoorde (of EDG)
admits that the EDG front end has a bug with this (which obviously
invalidates Caves' comment about Comeau C++ compiling the example fine).

Tom
 
...now playing the bouncing reopen-close game :(. If they just close it
Looks like they can be spoilsports! You could post a link to your clc++m
thread, in particular to the post where Daveed Vandevoorde (of EDG) admits
that the EDG front end has a bug with this (which obviously invalidates
Caves' comment about Comeau C++ compiling the example fine).

LOL - closed again. I am starting to feel that discussing via a
feedback-portal is annoying. Even if he would be right, it is unpolite to
just close the bug....

Regards,
Patrick

P.S.: IMO the new answer does not fit to the problem :).
 
Patrick said:
LOL - closed again. I am starting to feel that discussing via a
feedback-portal is annoying. Even if he would be right, it is unpolite to
just close the bug....

I've raised this with MS directly, so hopefully it will be resolved
properly this time...

Tom
 
Juhu, confirmed as a bug.

Is there a feedback portal for the feedback portal?

I would like to know how to format the comments, and how I can send a
private message to the responsible developer.

Regards,
Patrick
 
Patrick said:
Juhu, confirmed as a bug.
Hurrah!

Is there a feedback portal for the feedback portal?

I would like to know how to format the comments, and how I can send a
private message to the responsible developer.

You could try the "Contact Us" button at the bottom left of the page - I
suppose it probably counts as MSDN site feedback.

Tom
 
Back
Top