为什么编译器无法推导出模板参数std::forward?
我的意思是:
#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());
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个设计选择(由于std::remove_reference在定义中std::forward),以避免用户忘记指定类型.我不能得到的是:为什么它的实施方式可以防止扣除?为什么编译器不只是将forward模板参数推断为Arg.