4

My collegue just ran our codebase through a new compiler for a new platform, and unsurprisingly there were errors that had not been reported before.

One of them concerned code where the original author had accidentally typed a class name twice when calling a static function of that class. The looked like this:

class Thing
{
public:
    static void DoStuff ();
};

int main (int, char **)
{
    Thing::Thing::DoStuff();
    // or even: Thing::Thing::Thing::Thing::Thing::Thing::DoStuff();
    return 0;
}

GCC 4.9.2 on my local (Cygwin) machine compiles it fine, even with -Wall -Wextra -Wpedantic. A customized Clang that we're using for an embedded system also accepts it. Visual Studio 2012 (v110) gives me the error:

error C3083: '{ctor}': the symbol to the left of a '::' must be a type

I can imagine equally convincing arguments for:

  1. It's legal and the compiler accepts it. (i.e. GCC and Clang are correct, VS is wrong)
  2. It's not legal, but the compiler may accept it anyway.
  3. It's legal but the compiler doesn't accept it. (i.e. VS is wrong; maybe it's a new C++11 thing that hasn't been added yet?)
  4. It's not legal and the compiler shouldn't accept it. (i.e. Visual Studio is the only correct one)
  5. Just fix the error and get on with my life.

Which one is correct?

peterpi
  • 575
  • 2
  • 11
  • I hardly believe it should work since in the Thing class there is not another class or namespace called Thing. – Melkon Sep 18 '15 at 14:18
  • 5
    The name of the class is injected into the scope of the class itself, so this should be fine, even to arbitrary levels of repetition. – Kerrek SB Sep 18 '15 at 14:20
  • 8
    @Melkon yes there is : the [injected class name](http://stackoverflow.com/questions/25549652/c-why-is-there-injected-class-name). – Quentin Sep 18 '15 at 14:21
  • @Quentin: Whao, thanks, i didn't knew this. :o I think abusing this is avoidable tho, and i see no advantage of this. – Melkon Sep 18 '15 at 14:25
  • 4
    The injected class name is the answer, but there's a lot to be said for option 5. – aschepler Sep 18 '15 at 14:56

0 Answers0