Search code examples
c++namespacesfunction-declaration

Local function declaration inside namespace


In such a situation

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

Should the function another_function be defined inside the namespace n or outside? VS 2012 (with the November CTP) says it should be outside, and GCC 4.7.2 on the Mac says it should be inside. If I do the wrong one, I get undefined symbol errors from the linkers.

I generally trust GCC to be more compliant to the standard, but this is C++ and you can never be sure.


Solution

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

    7 When a block scope declaration of an entity with linkage is not found to refer to some other declaration, then that entity is a member of the innermost enclosing namespace. However such a declaration does not introduce the member name in its namespace scope.

    The declaration in your example declares n::another_function.