以下代碼是否合法? MSVC 9和g ++ 4.4不以爲然:派生類型可以與其基類的嵌套類型具有相同的名稱嗎?
struct base
{
struct derived {};
};
struct derived : base {};
int main()
{
typedef derived::derived type;
return 0;
}
MSVC抱怨,該類型的構造函數混淆了嵌套名稱:
c:\dev>cl test.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
test.cpp(10) : error C2146: syntax error : missing ';' before identifier 'type'
test.cpp(10) : error C2761: '{ctor}' : member function redeclaration not allowed
test.cpp(10) : error C2065: 'type' : undeclared identifier
雖然G ++並不:
$ g++ --version test.cpp
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
對於情況下,我的代碼包含一個名爲pointer
的迭代器。爲了提供迭代器接口,它提供了嵌套類型pointer
,這是它自己的同義詞。
我相信'''typename'''是違法的非模板代碼,或者至少一些編譯器抱怨關於它。 –
試一試'typedef typename derived :: derived type;'並查看它是否有效。 –
對不起,我誤解了你的代碼。順便說一句,對於Comeau,你的代碼是非法的(有或沒有'typename'),所以我傾向於認爲VC++就在這裏。 –