好吧,这可能看起来像一个愚蠢的问题,但它在这里:
template <typename T>
void foo(T& x)
{
}
int main()
{
foo(42);
// error in passing argument 1 of 'void foo(T&) [with T = int]'
}
Run Code Online (Sandbox Code Playgroud)
是什么阻止C++实例化实例化foo函数模板T = const int?
可能重复:
从rvalue参数推断对const的引用
如果我有
template<class T>
void foo(T &) { }
Run Code Online (Sandbox Code Playgroud)
我这样称呼它foo((const int)5),因为参数是const int,为什么不编译器自动推断T是const int?