在以下示例中,我有一個定義引用類的對象類。兩者都接受可變性作爲模板參數。在'obj'是'Const'的情況下,我想禁止'Non_Const'類型的引用。該示例在Visual C++ 2012中生成模糊消息「編譯器中發生了內部錯誤」。是否應編譯?如果沒有,爲什麼,還有另一種方法來完成同樣的事情?嵌套模板類型的部分專用化會在VC++ 2012編譯器中產生「內部錯誤」
enum Mutability {Const, Non_Const};
template <typename T, Mutability U>
class Obj
{
public:
template <Mutability V>
class Ref
{
public:
Ref() {}
friend class Obj;
};
Obj() {}
};
template <typename T>
class Obj<T, Const>::template Ref<Non_Const>
{
private:
Ref() {}
}; //error C1001: An internal error has occurred in the compiler
int main()
{
Obj<int, Const>::Ref<Non_Const> test;
}
C1001基本上意味着編譯器崩潰等,一方面這是一個編譯器錯誤(VS2013做同樣的事情),但它也無法使用gcc進行編譯:http://ideone.com/BhT1bB –
這非常糟糕 - 不是循環的,而是某種方式的循環聲明。 –