Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Are you asking why the compiler does the access check? 12.8/14 in C++03:</p> <blockquote> <p>A program is ill-formed if the copy constructor or the copy assignment operator for an object is implicitly used and the special member function is not accessible</p> </blockquote> <p>When the implementation "omits the copy construction" (permitted by 12.8/15), I don't believe this means that the copy ctor is no longer "implicitly used", it just isn't executed.</p> <p>Or are you asking why the standard says that? If copy elision were an exception to this rule about the access check, your program would be well-formed in implementations that successfully perform the elision, but ill-formed in implementations that don't.</p> <p>I'm pretty sure the authors would consider this a Bad Thing. Certainly it's easier to write portable code this way -- the compiler tells you if you write code that <em>attempts</em> to copy a non-copyable object, even if the copy happens to be elided in your implementation. I suspect that it could also inconvenience implementers to figure out whether the optimization will be successful <em>before</em> checking access (or to defer the access check until after the optimization is attempted), although I have no idea whether that warranted consideration.</p> <blockquote> <p>Could this behavior be dangerous? I mean, I might do some other useful thing in the copy-ctor, but if it doesn't call it, then does it not alter the behavior of the program?</p> </blockquote> <p>Of course it could be dangerous - side-effects in copy constructors occur if and only if the object is actually copied, and you should design them accordingly: the standard says copies can be elided, so don't put code in a copy constructor unless you're happy for it to be elided under the conditions defined in 12.8/15:</p> <pre><code>MyObject(const MyObject &amp;other) { std::cout &lt;&lt; "copy " &lt;&lt; (void*)(&amp;other) &lt;&lt; " to " &lt;&lt; (void*)this &lt;&lt; "\n"; // OK std::cout &lt;&lt; "object returned from function\n"; // dangerous: if the copy is // elided then an object will be returned but you won't see the message. } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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