相关疑难解决方法(0)

从rvalue参数中推导出对const的引用

好吧,这可能看起来像一个愚蠢的问题,但它在这里:

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

c++ templates const reference instantiation

10
推荐指数
1
解决办法
334
查看次数

为什么不将模板类型参数推断为'const'?

可能重复:
从rvalue参数推断对const的引用

如果我有

template<class T>
void foo(T &) { }
Run Code Online (Sandbox Code Playgroud)

我这样称呼它foo((const int)5),因为参数是const int,为什么不编译器自动推断Tconst int

c++ templates const type-parameter

7
推荐指数
2
解决办法
557
查看次数