C++ 外部常量变量不';t由函数调用初始化

C++ 外部常量变量不';t由函数调用初始化,c++,constants,extern,linkage,C++,Constants,Extern,Linkage,我以这种方式初始化两个外部常量变量: vars.hpp: int init_a(); extern const int a; extern const int b; vars.cpp: #include<vars.hpp> int init_a() { return 1; } const int a = init_a(); const int b = 1; stuff.cpp: const int d = a; const int e = b; main.cpp: #i

我以这种方式初始化两个外部常量变量:

vars.hpp:

int init_a();
extern const int a;
extern const int b;
vars.cpp:

#include<vars.hpp>
int init_a() {
    return 1;
}
const int a = init_a();
const int b = 1;
stuff.cpp:

const int d = a;
const int e = b;
main.cpp:

#include<vars.hpp>
#include<stuff.hpp>

int main(){
    std::cout << a << b << d << e << std::endl;
return 0;
}
#包括
#包括
int main(){

std::cout看起来像是静态初始化顺序的失败。这个重复是我能找到的最接近问题。其他人处理更复杂的场景和类型。如果你能找到它们,就发布更好的。你怎么知道它没有共享?你只在
main
中观察它。我不确定这是否真的是重复。这些问题的答案研究似乎表明输出应该是
1100
1111
,但事实并非如此。
#include<vars.hpp>
#include<stuff.hpp>

int main(){
    std::cout << a << b << d << e << std::endl;
return 0;
}