How does extern work in namespaces?

后端 未结 1 909
盖世英雄少女心
盖世英雄少女心 2021-02-04 14:38

I\'m running a simple program similar to what I found here. It\'s meant to reduce code bloat when including constants in multiple files. It does this by using const global varia

相关标签:
1条回答
  • 2021-02-04 14:52

    It's meant to reduce code bloat when including constants in multiple files

    I would suggest not to focus on this kind of optimizations unless it becomes really necessary, and rather choose the simplest design: define those constants directly in the header file and include that header file from all translation units (".cpp files") that need to access those constants. Since those objects are const, they will have internal linkage and the linker won't be screaming at you due to violations of the One Definition Rule.

    I tried removing the extern keywords in globals.cpp thinking that it wasn't needed, but my program won't run without them

    That's because namespace-scope const objects with static storage duration (like your pi variable) have internal linkage unless you explicitly define them as extern.

    I thought extern was only used for forward declarations?

    extern is used to declare a variable that is defined in another translation unit (".cpp file"). If the object is const, the translation unit that defines it needs to explicitly mark it as extern in order for it to have external linkage and be visible from other translation units (this wouldn't be necessary if the object were not const).

    Has it something to do with the namespace they are defined in?

    No, this is the rule for all namespace-level const objects with static storage duration, and it is specified in paragraph [basic.link]/3 of the C++ Standard:

    A name having namespace scope (3.3.6) has internal linkage if it is the name of

    (3.1) [...] — a variable, function or function template that is explicitly declared static; or,

    (3.2) — a variable of non-volatile const-qualified type that is neither explicitly declared extern nor previously declared to have external linkage; or

    (3.3) — a data member of an anonymous union.

    0 讨论(0)
提交回复
热议问题