Implicit copy constructor calls base with derived type under specific conditions. - by Noah Roberts

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 587787 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 8/20/2010 10:01:25 AM
Access Restriction Public


The implicit copy constructor passes the derived type rather than the base type to base copy constructors when using multiple inheritance and one or more base classes has a user defined copy constructor.
Sign in to post a comment.
Posted by Victor Scherba on 1/11/2012 at 7:52 AM
Hi, it sems to be solved
Posted by Noah Roberts on 8/31/2010 at 2:23 PM
Your explanation doesn't seem to jive with the steps required to reproduce the problem. Simply having the "base(derived const&)" constructor in the base type is not enough. You have to use multiple inheritance and one of the OTHER base classes needs to have a user-defined copy constructor. In the code I put in this report, if you do not derive from base2 you will not get an error. The program both compiles and functions correctly.

Your explanation DOES jive with behavior in older versions of the compiler where removing the inheritance of base2 doesn't cause the issue to go away.
Posted by Microsoft on 8/31/2010 at 2:02 PM
Thank you for reporting this issue. It is a well known bug that's unfortunately hard to fix without doing a lot of work in the compiler. Here's the reason:

In 12.8/8, bullet 1, the Standard says that for an implicitly defined (i.e., compiler generated) copy constructor, the *copy* constructor for the base shall be called to initialize it. This has always been implemented in the compiler by generating a token stream for the implicitly defined copy constructor as:

<class-name>::<class-name>(const <class-name> & __arg) : <base-name>(__arg) {}

The problem is that in this form, different rules apply because of the base-initializer list. The Standard in this case states that "<base-name>(__arg)" shall be treated as a direct-initialization and this is the behaviour that you are seeing where any suitable constructor from the base-class can be chosen, and not just the copy-constructors. The workaround that you have is a correct one and will force the compiler to do the right thing but unfortunately can't be used in all cases (otherwise this bug would be simple to fix).

Tanveer Gani
Visual C++
Posted by Microsoft on 8/22/2010 at 7:23 PM
Thank you for reporting the issue.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.These specialized experts will follow-up with your issue.
Posted by Microsoft on 8/20/2010 at 5:02 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(