7

The following namespace definition fails to compile when the first declaration is commented out. If the first declaration of foo is uncommented, then it compiles just fine.

namespace Y
{
    //void foo();
    void ::Y::foo(){}
}

The relevant part in the standard (§8.3¶1) says:

When the declarator-id is qualified, the declaration shall refer to a previously declared member

I understand that this rule prevents the introduction of names into other namespaces. I wonder if that rule could be relaxed to allow for qualified-ids referring to the current namespace.

Columbo
  • 60,038
  • 8
  • 155
  • 203
Hector
  • 2,464
  • 3
  • 19
  • 34
  • 1
    To the person who voted for closing because it is unclear what I am asking: The question is already in the text: *Is there any problem if that rule is relaxed to allow for qualified id's referring to the current space?* – Hector Nov 11 '15 at 01:15
  • 1
    Do you have a valid use-case for this rule-relaxation? – Karoly Horvath Nov 11 '15 at 01:28
  • @KarolyHorvath : define "valid" ;-) … the automatic generation of code could benefit if all identifiers could include their scopes. Note that this would be about programs that write programs not necessarily for human consumption. – Hector Nov 11 '15 at 01:38
  • 1
    That's funny, I actually thought about that;) But I still couldn't come up with a scenario. – Karoly Horvath Nov 11 '15 at 02:03

1 Answers1

4

CWG #482 is relevant:

According to 8.3 [dcl.meaning] paragraph 1, […]
This restriction prohibits examples like the following:

void f();
void ::f();        // error: qualified declarator

namespace N {
  void f();
  void N::f() { }  // error: qualified declarator
}

There doesn't seem to be any good reason for disallowing such declarations, and a number of implementations accept them in spite of the Standard's prohibition. Should the Standard be changed to allow them?

Notes from the April, 2006 meeting:

In discussing issue 548, the CWG agreed that the prohibition of qualified declarators inside their namespace should be removed.

So your code is valid if the first declaration of foo is present (as of about 2012; GCC has an open bug report). If not, however, your quoted wording still applies and renders the qualified declaration ill-formed. I see no reason to permit that case; it intuitively implies that the name has been declared already, since qualified name lookup must determine what it refers to.

Columbo
  • 60,038
  • 8
  • 155
  • 203