Note that there are some explanatory texts on larger screens.

plurals
  1. POC++0x perfect forwarding getting in the way of copy ctor?
    primarykey
    data
    text
    <p>Using MSVC2010...</p> <p>I have a struct that wraps a std::string, with standard move ctor's defined, as well as a perfect forwarding ctor to forward an argument to the std::string ctor.</p> <pre><code>struct Wrapper { std::string value; Wrapper() { } Wrapper( Wrapper const&amp; rhs ) :value(rhs.value) { } Wrapper( Wrapper&amp;&amp; rhs ) :value(std::move(rhs.value)) { } Wrapper&amp; operator=( Wrapper const&amp; rhs ) { value = rhs.value; return *this; } Wrapper&amp; operator=( Wrapper&amp;&amp; rhs ) { value = std::move(rhs.value); return *this; } template&lt;typename StringT&gt; Wrapper( StringT&amp;&amp; value ) :value(std::forward&lt;StringT&gt;(value)) { } }; </code></pre> <p>However now it seems I cannot copy-construct a Wrapper from another wrapper</p> <pre><code>Wrapper w0; Wrapper w1(w0); </code></pre> <p>This results in a compile error pointing at the perfect forwarding ctor, saying it cannot convert a Wrapper to a std::string. Is this correct behavior? Shouldn't the compiler call the copy ctor, versus the templated overload?</p> <pre><code>1&gt;t:\depot\warp\code\apps\pf_test\main.cpp(56): error C2664: 'std::basic_string&lt;_Elem,_Traits,_Ax&gt;::basic_string(const std::basic_string&lt;_Elem,_Traits,_Ax&gt; &amp;)' : cannot convert parameter 1 from 'Wrapper' to 'const std::basic_string&lt;_Elem,_Traits,_Ax&gt; &amp;' 1&gt; with 1&gt; [ 1&gt; _Elem=char, 1&gt; _Traits=std::char_traits&lt;char&gt;, 1&gt; _Ax=std::allocator&lt;char&gt; 1&gt; ] 1&gt; Reason: cannot convert from 'Wrapper' to 'const std::basic_string&lt;_Elem,_Traits,_Ax&gt;' 1&gt; with 1&gt; [ 1&gt; _Elem=char, 1&gt; _Traits=std::char_traits&lt;char&gt;, 1&gt; _Ax=std::allocator&lt;char&gt; 1&gt; ] 1&gt; No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1&gt; t:\depot\warp\code\apps\pf_test\main.cpp(63) : see reference to function template instantiation 'Wrapper::Wrapper&lt;Wrapper&amp;&gt;(StringT)' being compiled 1&gt; with 1&gt; [ 1&gt; StringT=Wrapper &amp; 1&gt; ] 1&gt; </code></pre> <p>If I define another copy ctor, one that takes a non-const reference to Wrapper (shown below), then everything seems fine... Is this the way moving forward? Or did I mess something up? Or is this a bug in VS2010?</p> <pre><code> Wrapper( Wrapper&amp; rhs ) :value(rhs.value) { } </code></pre>
    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