0

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

Greadimar
  • 81
  • 8
  • 3
    Why didn't you put the initialization of `aObj` in the initializer list like you do for the copy-constructor? – Some programmer dude Aug 24 '18 at 11:34
  • omg it works, thank you =0. I left it like this after some previous tests. But anyway still can't understand why it didn't work in body – Greadimar Aug 24 '18 at 11:38
  • 1
    It didn't work because you cannot initialize a member like that in the body. The compiler interprets the syntax as calling `operator()` on `aObj`, which fails because that doesn't exist. – Max Langhof Aug 24 '18 at 11:40
  • It doesn't work because you try to use the object `aObj` *as a function*. And the `SomeA` class doesn't have a function-call operator (`operator()`). – Some programmer dude Aug 24 '18 at 11:40
  • looks pretty obvious, thank you. – Greadimar Aug 24 '18 at 11:44

1 Answers1

0
 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.)

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157