Take the 2-minute tour ×

I'm running into a problem with initializing some const objects in my namespace. I have a namespace like the following:

namespace myNamespace{
    const std::string HI = "Hi";
    const std::string BYE = "Bye";

    inline std::vector<std::string> createHiAndByeVector(){
        std::vector<std::string> temp;
        return temp;

    const std::vector<std::string> HI_AND_BYE = createHiAndByeVector();

If I debug the initialization, I can see that both HI and BYE get assigned the string literals. The execution continues on to initialziae HI_AND_BYE, but when we get into the createHiAndByeVector() function, both HI and BYE have no values anymore. I then get a segmentation fault in the push_back() method. If I look at the call stack, I see the line: __static_initialization_and_destruction_0(). What is going on? Are my objects getting destroyed immediately after they're constructed?

share|improve this question
Why so cumbersome: You can just say std::vector<std::string> const HI_AND_BYE { "Hi", "Bye" };. –  Kerrek SB Feb 17 '13 at 1:36
All these definitions appear in the same translation unit (.cpp file)? –  Peter Ruderman Feb 17 '13 at 1:38
You have another static member depends on HI_AND_BYE? kind of static initialize order issue –  billz Feb 17 '13 at 1:57
@KerrekSB I didn't know that was possible! Thanks, that seems to work. However, is there one and only one vector created no matter how many translation units this header file is included in? –  Wagan8r Mar 15 '13 at 1:39
@Wagan8r: Well, you have to put the definition into the source file and only the declaration into the header... –  Kerrek SB Mar 15 '13 at 3:34
add comment

2 Answers

My guess is that the problem here is a violation of the One Definition Rule (ODR). The guessing here is that this code is actually in a header, which is also the reason you declared the function as inline.

Now, this code is compiled in multiple translation units (.cpp files), TU1 and TU2. This leads to two sets of both the constants and the inline function. Now, when linking, the constants exist independently of each other because they have internal linkage (const at namespace level causes this). However, the function does not have internal linkage, rather the linker is instructed to discard all instances but one via the inline. Now, the remaining one is used to initialize the vectors in both TU1 and TU2, but it uses the constant strings from one of them. Depending on when these are initialized, which is undefined, it can work or not. This is basically the initialization order fiasco that Deamonpog above mentions.

Getting back to the ODR, the problem is that the inline function is compiled twice but they are not the same, because they implicitly refer to different string constants. Similar problems arise when you have anonymous namespaces in headers. BTW: Other than that, this issue has nothing to do with namespaces! There are two ways to resolve this:

  1. Make the init function static, too. This separates all these constants and the function from their brethren in other translation units.
  2. You only declare (extern string const BYE;) the constants in a header and implement them (string const BYE = "Bye!";) in a separate TU. That way you only have a single instance of them that can be shared by different parts of your program.
share|improve this answer
Thanks for the detailed answer. I thought that functions within a namespace were implicitly, already static as were the two strings HI and BYE. In such case, why are there multiple "copies" of the strings? I'm trying to avoid option 2, as I'm making a header only library; in fact, that's exactly the scenario that I'm trying to rewrite. –  Wagan8r Mar 15 '13 at 1:38
add comment

I think this is due to the Initialization Fiasco. Following links may help you. The problem is that the Global/Static variables are initialized in a random manner. There is no particular order of initializing them. So maybe your HI_AND_BYE is initialized before HI or BYE is initialized.!!!

Finding C++ static initialization order problems


share|improve this answer
The initialization order of objects within a single translation unit is well-defined, it's in the order of their declaration. Note that e.g. the C++ FAQ at parashift's mentions objects "which exist in separate source files". –  Ulrich Eckhardt Feb 17 '13 at 10:20
aah, thank you for pointing that out :) –  Deamonpog Feb 17 '13 at 10:55
add comment

Your Answer


