1

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?

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

2 Answers2

6

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

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • 3
    The only document we have until publication is the final draft. The final draft has been unanimously accepted, but that doesn't stop it from being the final draft. :) – Lightness Races in Orbit Aug 15 '11 at 17:31
6

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.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168