我有一個編譯器錯誤的問題,看看下面的代碼:編譯器會忽略「常量」的功能參數
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