我在N3936(7.2.2條款)中讀到「在範圍枚舉的聲明中不應該省略可選標識符」,所以我嘗試了下列代碼 (其中嵌入的註釋試圖解釋我的解釋)與 GNU-G ++ 4.8.3和3.4.2鐺不透明和匿名枚舉聲明如何符合標準的要求?
# include <iostream>
enum any : int; // unscoped opaque declaration :int required by the standard
enum : int {a} t; // unscoped anonymous declaration of t (:int not required)
enum any : int {b} u; // redlecaration of type "any" with one enumerator
enum class foo : char; // scoped opaque declaration "foo" required, :char NOT
enum class foo : char {a, b} Foo; // redeclaration of "foo" with 2
// enumerators. now :char REQUIRED
enum class : char {d} Enum; // scoped anonymous declaration of Enum
// wouldn't be disallowed?
int main()
{
t = a; // assignment to "t"
u = b; // assignment to "u"
Foo = foo::a; // assignment to "Foo"
Enum = decltype(Enum)::d; // allowed (??)
std::cout << static_cast<int>(t) << ' '
<< static_cast<int>(u) << ' '
<< static_cast<int>(Foo) << ' '
<< static_cast<int>(Enum) << std::endl;
}
鐺拒絕的代碼,並在枚舉聲明發出一條消息,錯誤表示「作用域枚舉需要一個名稱」;然而,GNU-g ++接受 它並執行在標準輸出上放置四個零(正如預期的那樣,一旦代碼運行)。
注意鐺問題進行進一步的錯誤時,枚舉的名稱爲「d」是 改爲「一」,那樣的話,在這種情況下,錯誤地宣佈枚舉將 是未範圍枚舉名稱爲「」衝突與類型爲「any」的同名 (至少這是我解釋的讀取 診斷)。相反,GNU-g ++也會接受(連貫地)Enum枚舉器的名稱「a」 。
那麼,真相是什麼?
謝謝。我沒有發現這個錯誤。 – GSi 2015-02-09 09:28:37