none
move semantics problem

    Question

  • I'm trying to build a class with 3 constructors

    class not_a; class a {

    a(a&& v) {} a(const not_a& v) {} a(not_a&& v) {} }; a fkt() { not_a na_var; return na_var; // calls 'a(const not_a& v)' for conversion and then 'a(a&& v)' and not 'a(not_a&& v)' }


    Why is the "direct-move-constructor" not called? (using VS2010, I didn't try it on 2015 at the moment) ... should this be like that or is this not fully implemented in 2010?? ...

    Rudolf

    Monday, March 20, 2017 11:34 PM

Answers

  • On 3/20/2017 7:34 PM, Rudolf Meier wrote:

    I'm trying to build a class with 3 constructors

    class not_a;
    
    class a
    {        a(a&& v) {}
            a(const not_a& v) {}
            a(not_a&& v) {}
    };
    
    a fkt()
    {
            not_a na_var;
            return na_var; // calls 'a(const not_a& v)' for conversion and then 'a(a&& v)' and not 'a(not_a&& v)'
    }
    
    


    Why is the "direct-move-constructor" not called? (using VS2010, I didn't try it on 2015 at the moment) ... should this be like that or is this not fully implemented in 2010?? ...

    Works for me in VS2015 (http://rextester.com/NGN14085). I think it's just not correctly/fully implemented in VS2010. See whether the answer changes if you build Release rather than Debug. It's possible NRVO would kick in and give you the expected result.

    • Marked as answer by Rudolf Meier Tuesday, March 21, 2017 12:32 PM
    Tuesday, March 21, 2017 12:33 AM

All replies

  • On 3/20/2017 7:34 PM, Rudolf Meier wrote:

    I'm trying to build a class with 3 constructors

    class not_a;
    
    class a
    {        a(a&& v) {}
            a(const not_a& v) {}
            a(not_a&& v) {}
    };
    
    a fkt()
    {
            not_a na_var;
            return na_var; // calls 'a(const not_a& v)' for conversion and then 'a(a&& v)' and not 'a(not_a&& v)'
    }
    
    


    Why is the "direct-move-constructor" not called? (using VS2010, I didn't try it on 2015 at the moment) ... should this be like that or is this not fully implemented in 2010?? ...

    Works for me in VS2015 (http://rextester.com/NGN14085). I think it's just not correctly/fully implemented in VS2010. See whether the answer changes if you build Release rather than Debug. It's possible NRVO would kick in and give you the expected result.

    • Marked as answer by Rudolf Meier Tuesday, March 21, 2017 12:32 PM
    Tuesday, March 21, 2017 12:33 AM
  • If memory serves me correctly, the implementation in 2010 was incomplete. I remember a blog post mentioning that they implemented it and then the standards committee updated the standard. This made the implementation not conform to the standard and it was too close to the release to really fix it.

    This should have been fixed in VC2012 since that was when they updated the compiler to match the C++11 standard.

    Of course, I could be totally wrong about this and it was just a bug, but it still should have been fixed by VC2012.


    This is a signature Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Tuesday, March 21, 2017 6:08 AM