Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I advise against writing a general <code>strict_move</code> that is detecting <code>const</code>. I think that is not really what you're looking for. Do you want this to flag a <code>const complex&lt;double&gt;</code>, or a const <code>pair&lt;int, int&gt;</code>? These types will copy as fast they move. Flagging them would just be an irritant.</p> <p>If you want to do this, I recommend instead checking to see if the type is <code>noexcept MoveConstructible</code>. This will work perfectly for <code>std::string</code>. If the copy constructor of <code>string</code> is accidentally called, it is not noexcept, and therefore will be flagged. But if the copy constructor of <code>pair&lt;int, int&gt;</code> is accidentally called, do you really care?</p> <p>Here is a sketch of what this would look like:</p> <pre><code>#include &lt;utility&gt; #include &lt;type_traits&gt; template &lt;class T&gt; typename std::remove_reference&lt;T&gt;::type&amp;&amp; noexcept_move(T&amp;&amp; t) { typedef typename std::remove_reference&lt;T&gt;::type Tr; static_assert(std::is_nothrow_move_constructible&lt;Tr&gt;::value, "noexcept_move requires T to be noexcept move constructible"); static_assert(std::is_nothrow_move_assignable&lt;Tr&gt;::value, "noexcept_move requires T to be noexcept move assignable"); return std::move(t); } </code></pre> <p>I decided to check against <code>is_nothrow_move_assignable</code> as well, as you don't know whether the client is constructing or assigning the lhs.</p> <p>I opted for internal <code>static_assert</code> instead of an external <code>enable_if</code> because I don't expect <code>noexcept_move</code> to be overloaded, and the <code>static_assert</code> will yield a clearer error message when triggered.</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