Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can class members be defined outside the namespace in which they are declared?

Sometimes I find code like the following (actually some class-wizards create such code):

// C.h
namespace NS {

class C {
    void f();
};

}

and in the implementation file:

// C.cpp
#include "C.h"

using namespace NS;
void C::f() {
  //...
}

All the compilers I tried accept that kind of code (gcc, clang, msvc, compileonline.com). What makes me feel uncomfortable is the using namespace NS;. From my point of view C::f() lives in the global namespace in an environment that has unqualified access to objects living in namespace NS. But in the compiler's opinion void C::f() lives in namespace NS. As all compilers I tried share that point of view they are probably right, but where in the standard is this opinion backed?

like image 493
nriedlin Avatar asked Aug 07 '14 07:08

nriedlin


People also ask

Can we define member function outside the class?

Member functions and static members can be defined outside their class declaration if they have already been declared, but not defined, in the class member list. Nonstatic data members are defined when an object of their class is created.

How do you declare data members outside class?

You cannot declare member outside its class. You can define a member function outside of the class, if you have declared it inside. Second example simply defines a global function verify, which uses the public types from the class Screen, but itself is not a member of Screen.

Can we declare class inside namespace?

A class can also be declared inside namespace and defined outside namespace using the following syntax: CPP.

How members of a class can be defined inside and outside of a class?

A member function can be defined inside the class body where it is declared. Function's entire body is defined inside class body. A member function can be defined outside the class. To define a function outside the class, scope resolution operator is used.


1 Answers

Yes, the syntax is indeed legal, but no, your function actually does live in the namespace NS. The code you are seeing is actually equivalent to

namespace NS { void C::f() { /* ... } }

or to

void NS::C::f() { /* ... */ }

which may be more similiar to what you are used to.

Because of the using-directive you can omit the NS part not only in calling code, but also in its definition. The Standard has an example that matches your code (after the bold emphasized part):

3.4.3.2 Namespace members [namespace.qual]

7 In a declaration for a namespace member in which the declarator-id is a qualified-id, given that the qualified-id for the namespace member has the form nested-name-specifier unqualified-id the unqualified-id shall name a member of the namespace designated by the nested-name-specifier or of an element of the inline namespace set (7.3.1) of that namespace. [ Example:

namespace A {
  namespace B {
    void f1(int);
  }
  using namespace B;
}
void A::f1(int){ } // ill-formed, f1 is not a member of A

—end example ] However, in such namespace member declarations, the nested-name-specifier may rely on using-directives to implicitly provide the initial part of the nested-name-specifier. [ Example:

namespace A {
  namespace B {
    void f1(int);
  }
}

namespace C {
  namespace D {
    void f1(int);
  }
}

using namespace A;
using namespace C::D;
void B::f1(int){ } // OK, defines A::B::f1(int)

—end example ]

So you may omit the initial part of the nested-name-specifier, but not any intermediate part.

like image 185
TemplateRex Avatar answered Sep 20 '22 20:09

TemplateRex