Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Be very leery of the copy/swap assignment idiom. It can be sub-optimal, especially when applied without careful analysis. Even if you need strong exception safety for the assignment operator, that functionality can be otherwise obtained.</p> <p>For your example I recommend:</p> <pre><code>struct my_type { my_type(std::string name_) : name(std::move(name_)) {} void swap(my_type &amp;other) { name.swap(other.name); } private: std::string name; }; </code></pre> <p>This will get you implicit copy and move semantics which forward to std::string's copy and move members. And the author of std::string knows best how to get those operations done.</p> <p>If your compiler does not yet support implicit move generation, but does support defaulted special members, you can do this instead:</p> <pre><code>struct my_type { my_type(std::string name_) : name(std::move(name_)) {} my_type(const mytype&amp;) = default; my_type&amp; operator=(const mytype&amp;) = default; my_type(mytype&amp;&amp;) = default; my_type&amp; operator=(mytype&amp;&amp;) = default; void swap(my_type &amp;other) { name.swap(other.name); } private: std::string name; }; </code></pre> <p>You may also choose to do the above if you simply want to be explicit about your special members.</p> <p>If you're dealing with a compiler that does not yet support defaulted special members (or implicit move members), then you can explicitly supply what the compiler should eventually default when it becomes fully C++11 conforming:</p> <pre><code>struct my_type { my_type(std::string name_) : name(std::move(name_)) {} my_type(const mytype&amp; other) : name(other.name) {} my_type&amp; operator=(const mytype&amp; other) { name = other.name; return *this; } my_type(mytype&amp;&amp; other) : name(std::move(other.name)) {} my_type&amp; operator=(mytype&amp;&amp; other) { name = std::move(other.name); return *this; } void swap(my_type &amp;other) { name.swap(other.name); } private: std::string name; }; </code></pre> <p>If you really need strong exception safety for assignment, design it once and be explicit about it (edit to include suggestion by Luc Danton):</p> <pre><code>template &lt;class C&gt; typename std::enable_if &lt; std::is_nothrow_move_assignable&lt;C&gt;::value, C&amp; &gt;::type strong_assign(C&amp; c, C other) { c = std::move(other); return c; } template &lt;class C&gt; typename std::enable_if &lt; !std::is_nothrow_move_assignable&lt;C&gt;::value, C&amp; &gt;::type strong_assign(C&amp; c, C other) { using std::swap; static_assert(std::is_nothrow_swappable_v&lt;C&gt;, // C++17 only "Not safe if you move other into this function"); swap(c, other); return c; } </code></pre> <p>Now your clients can choose between efficiency (my type::operator=), or strong exception safety using <code>strong_assign</code>.</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.
    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