Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top