mardi 25 août 2015

Understanding std::forward

Why the compiler is not able to deduce the template parameter for std::forward?

I mean:

#include <memory>
#include <iostream>

struct X{};

struct A{
    A( const X& ) { std::cout << "cpy ctor\n"; }
    A( X&& ) { std::cout << "move ctor\n"; }
};

X foo() { return {}; }

template<typename T,typename Arg>
T* factory( Arg&& a )
{
    return new T(std::forward(a));
    // ----------^^^^^^^^^^^^^^^ error: can't deduce template parameter
}

int main()
{
    factory<A>(foo());
}

I know this is a design choice (due to the std::remove_reference in the definition of std::forward. What I can't get is why the compiler is not just deducing forward's template parameter as Arg.

Aucun commentaire:

Enregistrer un commentaire