vendredi 2 décembre 2016

std::move or std::forward when assigning universal constructor to member variable in C++

Consider the following classes foo1 and foo2

template <typename T>
struct foo1
{
    T t_;

    foo1(T&& t) :
        t_{ std::move(t) }
    {
    }
};

template <typename T>
struct foo2
{
    foo1<T> t_;

    foo2(T&& t) :
        t_{ std::forward<T>(t) }
    {
    }
};

Is it always the case that the constructor of foo1 represents the correct way to handle assigning the universal reference T&& to the member variable T? i.e. by using std::move.

Is it always the case that the constructor of foo2 represents the correct way to handle assigning the universal reference T&& to the member variable foo1<T> due to needing to forward to foo1's constructor? i.e. by using std::forward.

Aucun commentaire:

Enregistrer un commentaire