Search code examples
c++move-semanticsmove-constructorstdmove

std::move with inner objects - no match for call


Code below doesn't compile. Main:

#include "preset.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    SomeA a1(4);
    WrapA wA1(a1);
    WrapA wA2(std::move(wA1)); //fails to compile here
    return a.exec();
}

preset.h :

    #include <QDebug>
    class SomeA
    {
    public:
        SomeA(int l){val = l;}
        SomeA(const SomeA& original): val(original.val){qDebug()<<"sA copy;";}
        SomeA(SomeA&& original){std::swap(val, original.val); qDebug()<<"sA move;";}  
        SomeA operator = (const SomeA&);
        int val{0};
    };

    class WrapA
    {
    public:
        WrapA(SomeA obj): aObj(obj){}
        WrapA(const WrapA& original): aObj(original.aObj) {qDebug()<<"wA copy";}
        WrapA(WrapA&& original) {aObj(std::move(original.aObj)); qDebug()<<"wA move";} //no match for call (SomeA)(std::remove_reference<SomeA&>::type)
        SomeA aObj{0};
    };   

I suppose std::move doesn't cast from reference to rvalue. Any ideas how to achieve such a deep move c-tor? I guess I miss something, but can't understand what exactly


Solution

  •  WrapA(WrapA&& original) {aObj(std::move(original.aObj)); qDebug()<<"wA move";}
    

    You meant to write this as

     WrapA(WrapA&& original) : aObj(std::move(original.aObj)) {qDebug()<<"wA move";}
    

    See also the copy constructor for reference.

    (There must be a similar question to this already, but I can't find it.)