Note that there are some explanatory texts on larger screens.

plurals
  1. POimplicit vs. explicit deleted copy constructor
    primarykey
    data
    text
    <p>I've recently experienced some strange behaviour in my C++11 code.</p> <p>I've got a class, which is only movable:</p> <pre><code>class only_move { public: only_move() : value(0) {} only_move(int value) : value(value) {} only_move(const only_move&amp;) = delete; only_move&amp; operator=(const only_move&amp;) = delete; only_move(only_move&amp;&amp;) = default; only_move&amp; operator=(only_move&amp;&amp;) = default; int value; }; </code></pre> <p>And I've got another class, which contains an <code>only_move</code> object:</p> <pre><code>class has_only_move_member { public: has_only_move_member() = delete; has_only_move_member(only_move&amp;&amp; value) : member(std::move(value)) {} only_move member; }; </code></pre> <p>If I understood it correct, that means <code>has_only_move_member</code> can't be copied, as the <code>only_move</code> member can't be copied. This means <code>has_only_move_member(const has_only_move_member&amp;)</code> is deleted implicitly. Let's check that out:</p> <pre><code>has_only_move_member object(only_move(5)); has_only_move_member copy_of_object(object); </code></pre> <p>As expected, it prints out:</p> <pre><code>error: use of deleted function ‘has_only_move_member::has_only_move_member(const has_only_move_member&amp;)’ note: ‘has_only_move_member::has_only_move_member(const has_only_move_member&amp;)’ is implicitly deleted because the default definition would be ill-formed: class has_only_move_member </code></pre> <p>Okay, so I moved on and put <code>has_only_move_member</code> instances in a <code>std::map</code>. As they don't have a copy constructor, I've moved them into it:</p> <pre><code>has_only_move_member object(only_move(5)); std::map&lt;int, has_only_move_member&gt; data; data.emplace(5, std::move(object)); </code></pre> <p>So far so good. This works like a charm.</p> <p>But I had an idea. What about being more explicit and explicitly deleting the copy constructor of <code>has_only_move_member</code>. So I wrote into the class <code>has_only_move_member</code>:</p> <pre><code>has_only_move_member(const has_only_move_member&amp;) = delete; </code></pre> <p>After that, the same code from above, there I moved objects into an map gives me an error:</p> <pre><code>/usr/include/c++/4.8/bits/stl_pair.h:134:45: error: use of deleted function ‘has_only_move_member::has_only_move_member(const has_only_move_member&amp;)’ </code></pre> <p>Yeah of course it's deleted, but why is there such a gap between implicit and explicit delete?</p> <p>I'm getting the same behaviour with g++ 4.8.2 and clang 3.4-rc1 on Debian with libstdc++ and recent apple-clang-llvm 4.0 on Mac OS X 10.9 with libc++</p>
    singulars
    1. This table or related slice is empty.
    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