Consider the following translation unit:
extern "C" int x = 1;
I know that C-mangling an int
doesn't mean much; and that int x = 1
already guarantees external linkage, but this should work. Which it does; the thing is, GCC warns about using it, while clang doesn't: See this on GodBolt.
- Which compiler is "right"?
- How strongly, if it all would you advise avoiding such code?
According to the standard at [dcl.link.2]
Both brace-enclosed and as a prefix to a single declaration are valid.
Which compiler is correct? I'd say both, as they both accept the code and do the correct thing with it.
GCC's warning is just that, a warning. This is the same kind of warning as a "named parameter is not used anywhere": Information about something unusual, but not affecting the behavior of the program.