Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple copy constructors specified
    primarykey
    data
    text
    <p>With Visual C++ 2010, I have a class like this:</p> <pre><code>class MyClass{ public: MyClass(){} MyClass(MyClass &amp;){/*...*/} //A MyClass(const MyClass &amp;){/*...*/} //B template&lt;typename T&gt; MyClass(T &amp;&amp;t){ static_assert( !std::is_same&lt;typename std::remove_cv&lt;typename std::remove_reference&lt;T&gt;::type&gt;::type, MyClass&gt;::value, "Wrapping over wrapping is not allowed!"); } //C }; int main(int, char**){ MyClass a; const MyClass b(a); //assert fail if line A removed auto c=b; //assert fail if line B removed } //If both A and B exists //Warning C4521: multiple copy constructors specified //Furthermore, if both A and B removed //No error or warnings; VC2010 accepts peacefully. //In debug mode you will find the compiler generated trivial copy constructor </code></pre> <p>According to C++ standard, both line A and B are considered copy constructors, and C is a convert constructor. It is not surprising that I receive a warning that I declared multiple copy constructors. However, if I remove any of them, the static_assert fails and the code would not compile, that means the template constructor received control.</p> <p>I am sure that this behaviour follows the rule of function overloading. However is this a conflict of two rules? If A and B are copy constructors and one of them was declared, any attempt to copy the objects should not drop to the template, is it right?</p> <p>Update: According to N3242, 12.8.7,</p> <blockquote> <p>"a member function template is NEVER INSTANTIATED to perform the copy of a class object to an object of its class type."</p> </blockquote> <p>the correct implementation should be:</p> <ul> <li>No assert failure should occur when either A or B or both removed.</li> <li>If line B is removed, construction of c should fail because b is const.</li> <li>If both lines are removed, compiler should generate a copy constructor for the class.</li> <li>It is up to the implementation to warn the user if both lines exists.</li> </ul> <p>Any comment?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload