作为“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/