vendredi 21 avril 2017

Move constructor hasn't been called

Here is the code:

#include <memory>
#include <iostream>

template<typename T>
class Foo
{
public:
    Foo(T&& val) :
        val(std::make_unique<T>(
            std::forward<T>(val)))
    {
    }

    Foo(Foo&& that) :
        val(std::move(that.val))
    {
        std::cout << *val << std::endl;
    }

    std::unique_ptr<int> val;
};

template<typename T>
void Func(Foo<T>&& val)
{
    std::cout << *val.val << std::endl;
}

int main()
{
    Foo<int> instance(10);
    Func(std::move(instance));

    return 0;
}

The problem is that I expect two lines of output here (from my custom move constructor and from 'Func' function), but I get only one. Why?

Aucun commentaire:

Enregistrer un commentaire