[Bug c++/91808] New: Static definition rejected after extern declaration in anonymous namespace

andrey.vihrov at gmail dot com gcc-bugzilla@gcc.gnu.org
Wed Sep 18 14:28:00 GMT 2019


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91808

            Bug ID: 91808
           Summary: Static definition rejected after extern declaration in
                    anonymous namespace
           Product: gcc
           Version: 9.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrey.vihrov at gmail dot com
  Target Milestone: ---

Consider the following C++11 sample:

    namespace
    {
        extern int x;
        static int x = 0;
    }

Compiling this with "gcc -c test.cpp" gives

test.cpp:4:16: error: '{anonymous}::x' was declared 'extern' and later 'static'
[-fpermissive]
    4 |     static int x = 0;
      |                ^
test.cpp:3:16: note: previous declaration of '{anonymous}::x'
    3 |     extern int x;
      |                ^

According to [1], point 3.5.4, the "extern int x;" declaration has the same
linkage as its enclosing namespace, which is anonymous and thus has internal
linkage. According to point 3.1.2, this declaration is not a definition.

According to point 3.5.3, the "static int x = 0;" declaration has internal
linkage. It is also the first definition for the internal "int {anonymous}::x"
variable. Overall, this declaration doesn't conflict with the previous one.

Note: Clang (starting with version 6) and MSVC accept the code.

[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf


More information about the Gcc-bugs mailing list