Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange behavior of copy-initialization, doesn't call the copy-constructor!
    text
    copied!<p>I was reading the difference between direct-initialization and copy-initialization (§8.5/12):</p> <pre><code>T x(a); //direct-initialization T y = a; //copy-initialization </code></pre> <p>What I understand from reading about <a href="https://stackoverflow.com/questions/1051379/is-there-a-difference-in-c-between-copy-initialization-and-direct-initializatio">copy-initialization</a> is that it needs <a href="https://stackoverflow.com/questions/4293596/when-should-you-use-direct-initialization-and-when-copy-initialization">accessible &amp; non-explicit copy-constructor</a>, or else the program wouldn't compile. I verified it by writing the following code:</p> <pre><code>struct A { int i; A(int i) : i(i) { std::cout &lt;&lt; " A(int i)" &lt;&lt; std::endl; } private: A(const A &amp;a) { std::cout &lt;&lt; " A(const A &amp;)" &lt;&lt; std::endl; } }; int main() { A a = 10; //error - copy-ctor is private! } </code></pre> <p>GCC gives an error (<a href="http://www.ideone.com/fB9K5" rel="nofollow noreferrer">ideone</a>) saying:</p> <blockquote> <p>prog.cpp:8: error: ‘A::A(const A&amp;)’ is private</p> </blockquote> <p>So far everything is fine, <a href="http://www.gotw.ca/gotw/036.htm" rel="nofollow noreferrer">reaffirming what Herb Sutter says</a>,</p> <blockquote> <p>Copy initialization means the object is initialized <strong>using the copy constructor</strong>, after first calling a user-defined conversion if necessary, and is equivalent to the form "T t = u;":</p> </blockquote> <hr> <p>After that I made the copy-ctor accessible by commenting the <code>private</code> keyword. Now, naturally I would expect the following to get printed:</p> <blockquote> <p>A(const A&amp;)</p> </blockquote> <p>But to my surprise, it prints this instead (<a href="http://www.ideone.com/oEXUT" rel="nofollow noreferrer">ideone</a>):</p> <blockquote> <p>A(int i)</p> </blockquote> <p>Why?</p> <p>Alright, I understand that first a temporary object of type <code>A</code> is created out of <code>10</code> which is <code>int</code> type, by using <code>A(int i)</code>, applying the conversion rule as its needed here (§8.5/14), and then it was supposed to call copy-ctor to initialize <code>a</code>. But it didn't. Why? </p> <p>If an implementation is permitted to eliminate the need to call copy-constructor (§8.5/14), then why is it not accepting the code when the copy-constructor is declared <code>private</code>? After all, its not calling it. Its like a spoiled kid who first irritatingly asks for a <em>specific</em> toy, and when you give him one, the <em>specific</em> one, he throws it away, behind your back. :|</p> <p>Could this behavior be dangerous? I mean, I might do some other <em>useful</em> thing in the copy-ctor, but if it doesn't call it, then does it not alter the behavior of the program?</p>
 

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