jeudi 28 juillet 2022

const lvalue reference vs forwarding reference

I just cannot understand why would one want to use a forwarding reference instead of a const lvalue reference.

So why instead of this:

template<typename T>
void g(T&& x)
{
    ...
}

template<typename T>
void f(T&& x)
{
    g(std::forward<T>(x));
}

I could not use the following and get the same results performance wise (assuming I dont want to modify object x):

template<typename T>
void g(const T& x)
{
    ...
}

template<typename T>
void f(const T& x)
{
    g(x);
}

Aucun commentaire:

Enregistrer un commentaire