C++11 gcc : explicit qualification in declaration? 标准引用?

标签 c++ gcc c++11

当使用 gcc 4.7 编译以下 C++11 程序时:

extern int i;
int ::i;

int main()
{
}

gcc 提示说:

error: explicit qualification in declaration of `i`

这是不符合规范的行为吗?该程序在标准中的何处被视为格式错误?

8.3p1好像表示应该允许:

If the qualifier is the global :: scope resolution operator, the declarator-id refers to a name declared in the global namespace scope.

更新:

来自 N3485 8.3p1:

A list of declarators appears after an optional (Clause 7) decl-specifier-seq (7.1). Each declarator contains exactly one declarator-id; it names the identifier that is declared. An unqualified-id occurring in a declarator- id shall be a simple identifier except for the declaration of some special functions (12.3, 12.4, 13.5) and for the declaration of template specializations or partial specializations (14.7). When the declarator-id is qualified, the declaration shall refer to a previously declared member of the class or namespace to which the qualifier refers (or, in the case of a namespace, of an element of the inline namespace set of that namespace (7.3.1)) or to a specialization thereof; the member shall not merely have been introduced by a using-declaration in the scope of the class or namespace nominated by the nested-name-specifier of the declarator-id. The nested-name-specifier of a qualified declarator-id shall not begin with a decltype-specifier. [ Note: If the qualifier is the global :: scope resolution operator, the declarator-id refers to a name declared in the global namespace scope. — end note ] The optional attribute-specifier-seq following a declarator-id appertains to the entity that is declared.

最佳答案

紧接着的下一句话(在 n3337 中):

A declarator-id shall not be qualified except for the definition of a member function or static data member outside of its class, the definition or explicit instantiation of a function or variable member of a namespace outside of its namespace, or the definition of an explicit specialization outside of its namespace, or the declaration of a friend function that is a member of another class or namespace.

异常(exception)中没有提到全局变量的定义。

关于C++11 gcc : explicit qualification in declaration? 标准引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17958497/

相关文章:

c++ - boost unordered_set 的意外行为

c - PIC(位置无关代码)

c++ - 为什么在析构函数中抛出异常时不调用重载删除?

c++ - 为 conio.h 使用另一个 header

c++ - 使用模板结构作为命名空间

c++ - 在 C++ 中使用按引用和按值传递的变量的时机

C++ 什么决定了哪个版本的 C++ 可以在特定架构(如 Arduino)上运行

objective-c - XCode GCC-4.0 与 4.2

c - 使用 clang 与 gcc 进行 union 零初始化

c++ - 设置一个 std::function 变量来引用 std::sin 函数