【发布时间】:2018-11-25 02:23:33
【问题描述】:
在下面的代码中,为什么调用fun:fun(num)和fun<const int>(num)这两种方式在编译时会给出不同的结果?
#include <iostream>
using namespace std;
template<typename T, typename = typename enable_if<!std::is_same<int, T>::value>::type>
void fun(T val)
{
cout << val << endl;
}
int main(void)
{
const int num = 42;
fun(num); //ERROR!
fun<const int>(num); //Right
return 0;
}
【问题讨论】:
-
intprvalue 永远不会被 const 限定。
标签: c++ templates constants template-function template-argument-deduction