我有一个编译器错误的问题,看看下面的代码:编译器会忽略“常量”的功能参数
template<class T>
struct MyStruct
{
};
template<>
struct MyStruct<int>
{
typedef int* type;
};
template<class T>
void foo(const typename MyStruct<T>::type myType)
{
}
int main()
{
const int* ptr = NULL;
foo<int>(ptr);
return 0;
}
的问题是,编译器无视于foo功能“常量”,使foo调用非法(const int * to int *)。
严重性代码说明项目文件的线路抑制状态 错误C2664 '无效美孚(常量MYSTRUCT ::类型)':不能转换参数1 'const int的*' 到 '常量MYSTRUCT ::型'
我在Visual Studio和gcc的5.3编译器中测试了以下代码,它们都丢失了相同的错误。
编译器是否故意这样做?为什么发生这种情况?
'const int * ptr'不是一个常量指针,它是一个指向const的指针。 – juanchopanza