Is qualified name in the member function declaration allowed?

13,112

Solution 1

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.

Solution 2

As I understand it is Not valid as per the C++03 Specification.

Reference - C++03 standard:

Section $8.3:

Each declarator contains exactly one declarator-id; it names the identifier that is declared. The id-expression of a declarator-id shall be a simple identifier except for the declaration of some special functions (12.3, 12.4, 13.5) and for the declaration of template specializations or partial specializations (14.7). A declarator-id shall not be qualified except for the definition of a member function (9.3) or static data member (9.4) or nested class (9.7) outside of its class, the definition or explicit instantiation of a function, variable or class 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 (11.4).

I hope I am deriving the appropriate meaning of the above. I will admit reading & understanding the quotes from the Standard makes me a little dizzy. Let me know if I interpret it wrongly.

Share:
13,112
Armen Tsirunyan
Author by

Armen Tsirunyan

Updated on June 17, 2022

Comments

  • Armen Tsirunyan
    Armen Tsirunyan almost 2 years

    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();
    };
    
  • Armen Tsirunyan
    Armen Tsirunyan over 12 years
    Thank you, James, your answer is perfect, but Als was twelve seconds faster, so I accepted his answer :)
  • Alok Save
    Alok Save over 12 years
    My +1, Because this answer has better explanation of quote from the standard, and it will be even understandable by Non Standerdese fans.If I was Armen I would accept this as the answer.
  • Johannes Schaub - litb
    Johannes Schaub - litb over 12 years
    12 seconds faster and 12 percent less beautifully formatted :)
  • Armen Tsirunyan
    Armen Tsirunyan over 12 years
    @Als, OK then, if you don't mind, then, I'll accept this one :)
  • Alok Save
    Alok Save over 12 years
    @Armen Tsirunyan: I don't mind at all, Infact, Happy that you made the right choice. I believe The answer which is correct & Best explains the problem & the solution to it should be accepted not the first correct answer. :)
  • Armen Tsirunyan
    Armen Tsirunyan over 12 years
    @Als: To be perfectly honest, both your answers explain the issue in its entirety, it's difficult to compare two good answer