Search code examples
c++visual-c++c++17language-lawyer

extern const struct VS struct extern const


Consider:

flavor1.h

extern const struct X {
    int i = 1;
} x;

flavor2.h

struct Y {
    int i = 1;
} extern const y;

impl.cpp

#include "flavor1.h"
#include "flavor2.h"

const X x;
const Y y;

If I try to compile : 'const Y y' : redéfinition (redefinition) - even tho y is declared extern.

My question : what am I actually declaring in flavor2.h ? I thought it would be equivalent to flavor1.h.


Solution

  • Looks like a bug in MSVC.

    Your code is valid. As you are saying, there is only one definition of y. The first declaration of y is not a definition because of the use of extern.

    GCC and Clang also don't have a problem with the code.

    And yes, the order of the decl-spcifiers extern, const and struct X { int i = 1; } in the declaration should not matter. They can be ordered in any way you like.