c++ - 基本说明符中的全局限定

标签 c++ c++11

作为“What is this crazy C++11 syntax ==> struct : bar {} foo {};?”的后续,我希望编译以下 C++0x 代码:

struct x {};
struct :::x {} y {};

但是,GCC 4.7.0 20110731 告诉我:

error: global qualification of class name is invalid before ':' token

当我恢复理智并给第二个 UDT 命名时:

struct x {};
struct a:::x {} y{}; // remember, identical to `a::: x` or `a: ::x` or `a:: :x` etc

错误是:

error: 'a' has not been declared


这三个冒号似乎被词法化为 <::> <:>而不是 <:> <::> ,但这是否可以从[最终草案]标准中清楚地推导出来?

与问题“Global qualification in a class declarations class-head”是否相关?

最佳答案

这只是与解析有关。来自§2.5.3

If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token, even if that would cause further lexical analysis to fail.

基本上,它必须取最长的字符序列,所以 ::: 总是被解析为 :: : x+++y 总是被解析为 x ++ + y.

这称为 Maximal Munch解析。

关于c++ - 基本说明符中的全局限定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7068260/

相关文章:

c++ - 编译器如何根据优先级和关联性来解释这个表达式?

c++ - 使用 getline 处理重定向文件输入后使用 cin 进行键盘输入

c++ - 具有不同签名的函数数组

c++ - 什么是像 std::atomic_is_lock_free 这样的 c 风格原子自由函数?

android - C++11 cmath 函数不在带有 gcc-4.8 或 clang 3.4 的 android NDK 的 std 命名空间中

c++ - 在 C++ 中包含头文件时尖括号 < > 和双引号 ""之间的区别?

c++ - 使用 C 的 '...' 参数包作为 C++ 模板值?

c++ - 使用 char** argv 时如何避免指针运算

C++: vector 边界

c++ - 类型转换、显式构造函数等