7

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
  • 130,161
  • 59
  • 324
  • 434

2 Answers2

13

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Thank you, James, your answer is perfect, but Als was twelve seconds faster, so I accepted his answer :) – Armen Tsirunyan Aug 18 '11 at 18:54
  • 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. – Alok Save Aug 18 '11 at 19:05
  • 12 seconds faster and 12 percent less beautifully formatted :) – Johannes Schaub - litb Aug 18 '11 at 19:05
  • @Als, OK then, if you don't mind, then, I'll accept this one :) – Armen Tsirunyan Aug 18 '11 at 19:19
  • @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.** :) – Alok Save Aug 18 '11 at 19:23
  • @Als: To be perfectly honest, both your answers explain the issue in its entirety, it's difficult to compare two good answer – Armen Tsirunyan Aug 18 '11 at 19:28
7

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533