mercredi 5 avril 2017

Passing std::unique_ptr to constructor to take ownership

I want to pass a std::unique_ptr to the constructor of a class that will take ownership of the data owned by the std::unique_ptr.

Are there any differences between the approaches foo and bar below in terms of how the compiler handles them that would make one of them preferable?

foo class:

template <class T>
class foo
{
    std::unique_ptr<T> data_;

public:
    foo(std::unique_ptr<T>&& data) :
        data_{ std::forward<std::unique_ptr<T>>(data) }
    {
    }
};

bar class:

template <class T>
class bar
{
    std::unique_ptr<T> data_;

public:
    bar(std::unique_ptr<T> data) :
        data_{ std::move(data) }
    {
    }
};

Aucun commentaire:

Enregistrer un commentaire