Reputation: 385224
As a follow-up to " What is this crazy C++11 syntax ==> struct : bar {} foo {};? ", I'd expect the following C++0x code to compile:
struct x {};
struct :::x {} y {};
However, GCC 4.7.0 20110731 tells me:
error: global qualification of class name is invalid before ':' token
And when I take a step back towards sanity and give the second UDT a name:
struct x {};
struct a:::x {} y{}; // remember, identical to `a::: x` or `a: ::x` or `a:: :x` etc
the error is:
error: 'a' has not been declared
It seems like the three colons are being lexed like <::> <:>
rather than <:> <::>
, but can this clearly be deduced from the [final draft] standard?
And might the question " Global qualification in a class declarations class-head " be related?
Upvotes: 1
Views: 665
Reputation: 54280
This is just to do with parsing. From §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.
Basically, it has to take the longest sequence of characters, so :::
is always parsed as ::
:
in the same way that x+++y
is always parsed as x
++
+
y
.
This is referred to as Maximal Munch parsing.
Upvotes: 6
Reputation: 133044
It seems like the three colons are being lexed like <::> <:> rather than <:> <::>, but can this clearly deduced from the [final draft] standard?
It's not a [final] draft any more. It has been unanimously accepted already. And yes, it can be deduced that :::
will be parsed as ::
followed by :
. It's a purely lexical issue. As in C++03, the token is taken as the longest possible one. In C++03 that led template closing >>
s to be considered as shift operator. In C++0x an exception had been made for this special case, but the general rule still applies (See 2.5.3). For example +++ will be parsed as ++ followed by +, not vice versa
Upvotes: 6