.NET Forum / Languages / Managed C++ / April 2006
VS2005 - question about copy-ctor
|
|
Thread rating:  |
Patrick Kowalzick - 24 Mar 2006 10:44 GMT 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; }
Tom Widmer [VC++ MVP] - 24 Mar 2006 12:15 GMT > Hello all, > [quoted text clipped - 3 lines] > 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() > { [quoted text clipped - 5 lines] > return 0; > } You may want to report the bugs to MS.
Tom
Patrick Kowalzick - 24 Mar 2006 12:51 GMT Hello Tom,
> I agree that it shouldn't compile, and for 2 separate reasons: > [quoted text clipped - 9 lines] > 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.
>> class A : public noncopyable // inherit to avoid copying >> { [quoted text clipped - 6 lines] > { > }; 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
Bruno van Dooren - 24 Mar 2006 14:16 GMT >> 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. 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.
 Signature Kind regards, Bruno van Dooren bruno_nos_pam_van_dooren@hotmail.com Remove only "_nos_pam"
Patrick Kowalzick - 24 Mar 2006 14:55 GMT Hello all,
>>> You may want to report the bugs to MS. >> [quoted text clipped - 6 lines] > 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
Tom Widmer [VC++ MVP] - 24 Mar 2006 16:28 GMT > Hello all, > [quoted text clipped - 17 lines] > 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
Carl Daniel [VC++ MVP] - 24 Mar 2006 17:02 GMT > 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 [quoted text clipped - 16 lines] > Bar b; > } It does compile with VC8. Looks like another bug is needed.
-cd
Patrick Kowalzick - 27 Mar 2006 09:11 GMT > 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 [quoted text clipped - 15 lines] > Bar b; > } I posted it here:
http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackId=FDBK47867
I messed up the class namig, but it should be understandable :).
Regards, Patrick
Tom Widmer [VC++ MVP] - 28 Mar 2006 11:59 GMT >>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 [quoted text clipped - 21 lines] > > I messed up the class namig, but it should be understandable :). Great. It's about time I installed VC8 I think, then I could at least validate the bug.
Tom
Patrick Kowalzick - 03 Apr 2006 09:02 GMT Hello all,
>>>>>You may want to report the bugs to MS. >>>> [quoted text clipped - 15 lines] >> but I did not manage it. I opened a new one, because the latter one >> refers to the beta-version. 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
Tom Widmer [VC++ MVP] - 03 Apr 2006 10:35 GMT > Hello all, Hello.
>>>I posted it here: >>>http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackid=FDBK47765 [quoted text clipped - 13 lines] > > 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
Patrick Kowalzick - 03 Apr 2006 15:46 GMT > 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
Patrick Kowalzick - 21 Apr 2006 07:14 GMT ...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
>> I've posted a comment to the bug suggesting that the resolution is >> incorrect. [quoted text clipped - 4 lines] > Regards, > Patrick Tom Widmer [VC++ MVP] - 21 Apr 2006 13:53 GMT > ...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
Patrick Kowalzick - 24 Apr 2006 07:22 GMT >> ...now playing the bouncing reopen-close game :(. If they just close it >> another time without argueing, I'll drop it. [quoted text clipped - 5 lines] > 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 :).
Tom Widmer [VC++ MVP] - 25 Apr 2006 10:40 GMT >>>...now playing the bouncing reopen-close game :(. If they just close it >>>another time without argueing, I'll drop it. [quoted text clipped - 9 lines] > 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
Patrick Kowalzick - 25 Apr 2006 12:19 GMT > I've raised this with MS directly, so hopefully it will be resolved > properly this time... And I reopened and added another point of view ;).
Patrick
Patrick Kowalzick - 27 Apr 2006 14:02 GMT 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
>>>>...now playing the bouncing reopen-close game :(. If they just close it >>>>another time without argueing, I'll drop it. [quoted text clipped - 14 lines] > > Tom Tom Widmer [VC++ MVP] - 28 Apr 2006 10:25 GMT > 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
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|