Declaring a local function inside a namespace

In such situation

namespace n { void f() { void another_function(); } } 

Should another_function be defined inside the n namespace or outside? VS 2012 (since November CTP ) says it should be outside, and GCC 4.7.2 on Mac says it should be inside. If I am mistaken, I get undefined character errors from linkers.

I usually find GCC more compatible with the standard, but it's C ++, and you can never be sure.

+7
source share
2 answers

C ++ 11 3.5 (as well as C ++ 03)

7 If the declaration of the scope of the object with the binding is not found to refer to some other expression, then this organization is a member of the innermost namespace. However, such a declaration does not enter a member name in the namespace area.

The declaration in your example declares n::another_function .

+11
source

According to N3485 7.3.1 [namespace.def] / 6 the correct answer is n::another_function .

Incoming declaration namespaces are those namespaces in which the declaration lexically appears, with the exception of a member of the namespace outside of its original namespace (for example, the definition as specified in 7.3.1.2). This renewal has the same namespaces as the original ad. [Example:

 namespace Q { namespace V { void f(); // enclosing namespaces are the global namespace, Q, and Q::V class C { void m(); }; } void V::f() { // enclosing namespaces are the global namespace, Q, and Q::V extern void h(); // ... so this declares Q::V::h } void V::C::m() { // enclosing namespaces are the global namespace, Q, and Q::V } } 

-end example]

+3
source

All Articles