views:

771

answers:

2

Hi.

Simple question, is this valid C++:

class Foo
{
    void Foo::doSomething();
};

The point of the question: is that repeated use of the class name and double colon before the method name valid inside the class declaration?

I'm having issues compiling code that does this using g++ 4.2.3. I would love to see a reference to something describing the syntax here, before digging in and changing the code. Or downgrading the compiler; this does build with g++ 3.3.6.

The error I'm getting is (roughly):

Foo.h:3: error: extra qualification ‘Foo::’ on member ‘doSomething’

I did Google, but couldn't come up with something. I don't have the standard, and even if I did it would probably take me quite a while to find anything authoritative. I'm no C++ language lawyer.

+17  A: 

I took a look at the standard, section 9.2 would be the relevant portion. I'm not that great with BNF but I didn't see anything in the BNF for class members that would indicate this was allowed. The identifier is even named "unqualified-id" in the BNF.

G++ changed the behavior in version 4.1, and apparently a lot of other compilers accepted this, but I've never seen this style used and I have no idea why anyone would do it. Since it seems to not trigger an error on a pretty wide variety of compilers, there may be some historical reason for this style, but as far as I can tell it's indeed not valid.

The only good reference I found through Google was this page, which just attempts to explain some of the changes in G++ 4.1.

Dan Olson
Thanks, this is a very helpful answer. I've voted it up. I'll hold on a while before accepting, hoping to help people feel motivated enough to do more standards-searching. :)
unwind
I don't know why anyone would want to use this syntax either, but i know how it can magically appear. Sort of. Copy/paste from cpp to header file to inline the function and there you are.
Benoît
Yes, that's what I suspect happened, too.
unwind
+4  A: 

Like Dan, I looked at the Standard without definitive results. I tried your code with Comeau's on-line compiler (considered the world's most standard compliant) and got:

line 3: error: qualified name is not allowed in member declaration

If you are interested in taking this further, I suggest posting a question on the comp.lang.c++.moderated newsgroup, as there are a lot more C++ language lawyers there than there are here.

anon
Thanks! I voted this one up, but have now accepted Dan's answer, which I think makes sense and is fair.
unwind
I also use Comeau compiler for standard checking (and error reporting, as it is more expressive). That is a good advice to anyone: check code snippets with Comeau (online or buy it, as it is rather cheap)
David Rodríguez - dribeas