[英]Function Template: typename declaration
在GCC上,以下內容給我一個錯誤: no type named 'x' in 'struct Type'
在VC ++上,它抱怨未聲明p
struct Type
{
static int const x = 0;
};
template <class T> void Func()
{
typename T::x * p; // p to be pointer
}
int main()
{
Func<Type>();
}
T::x
變成Type::x
,它是一個int
,而不是類型。
您已經告訴編譯器, T::x
通過使用typename
命名類型。 實例化Func<Type>
, T::x
不是類型,因此編譯器報告錯誤。
在C ++ 11中,using關鍵字可用於類型別名
struct Type
{
using x = static int const;
};
由於Type::x
不是一個類型 ,而是一個值 ,所以當你寫typename
,你告訴編譯器找到嵌套的類型名稱為x
的內部Type
,但它不能。 因此,GCC表示no type named 'x' in 'struct Type'
這比VC ++生成的消息更有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.