Is qualified name in the member function declaration allowed?

Armen Tsirunyan picture Armen Tsirunyan · Aug 18, 2011 · Viewed 12.8k times · Source

This code is accepted by MSVC9.0. My question is whether it is legal according to the standard (the old and/or the new one). A quote would be very much welcome, too.

class X
{
   void X::f();
};

Answer

James McNellis picture James McNellis · Aug 18, 2011

No, this is not valid. Here, X::f is a qualified name; you are attempting to use it as a declarator-id. C++03 8.3[dcl.meaning]/1 lists the circumstances under which a declarator-id may be qualified:

A declarator-id shall not be qualified except for

  • the definition of a member function or static data member outside of its class,

  • the definition or explicit instantiation of a function or variable member of a namespace outside of its namespace, or

  • the definition of a previously declared explicit specialization outside of its namespace, or

  • the declaration of a friend function that is a member of another class or namespace.

Because X::f falls into none of these four categories, it is incorrect.

The rule that requires the definition of a member function outside of the class definition to be qualified can be found at C++03 9.3[class.mfct]/5:

If the definition of a member function is lexically outside its class definition, the member function name shall be qualified by its class name using the :: operator.