Search code examples
c++namespacesg++inline

inline namespace and extension namespace


I read the section about namespace definition. Clause 7.3.1 of N3797 said:

The inline keyword may be used on an extension-namespace-definition only if it was previously used on the original-namespace-definition for that namespace.

Consider the following code snippet:

namespace M
{
    int h;
}

inline namespace M
{
    int j = 6;
}

It compiled successful both with the -std=c++11 and without that option. Can you explain that behavior? Is it a g++ bug?


Solution

  • Vour reference to the standard is explicit: this is not allowed.

    Using Clang++ I get the very clear error message about this:

    Test0614-1.cpp:17:18: error: non-inline namespace cannot be reopened as inline
    inline namespace M
                     ^
    Test0614-1.cpp:12:11: note: previous definition is here
    namespace M
              ^
    

    So it's definitively a bug in g++. By the way, it's reported here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53402

    The fact that the compiler accepts inline namespace for previous version of the standard and without at least a warning seems to be an issue. This was already reported as bug in 2010 and should have been fixed: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43824