jeudi 6 septembre 2018

How to move temporary object without std::move

Move constructor of class accepts rvalue reference which can be reference to temporary object. So, i have temporary object and appropriate move constructor but that move constructor does not called. What`s wrong?

#include <iostream>

class foo
{
    int data;
public:
    foo(int v) : data(v) {std::cout << "foo(int)\n";}

    foo(foo&& f)
    {
        std::cout << "moved\n";
    }

    void print()
    {
        std::cout << data;
    }
};

int main()
{
    foo f1 = foo(100);//create from temporary
    f1.print();
}

Aucun commentaire:

Enregistrer un commentaire