如果存在依赖,编译器能否正确处理静态变量的初始化顺序? [英] Can the compiler deal with the initialization order of static variables correctly if there is dependency?
本文介绍了如果存在依赖,编译器能否正确处理静态变量的初始化顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果存在依赖,编译器能否正确处理静态变量的初始化顺序?例如,我有
Can the compiler deal with the initialization order of static variables correctly if there is dependency? For example, I have
a.h
:
struct A { static double a; };
a.cpp
:
#include "a.h"
double A::a = 1;
b.h
:
struct B { static double b; };
b.cpp
:
#include "b.h"
#include "a.h"
double B::b = A::a;
推荐答案
在翻译单元中指定了这种初始化的顺序.未指定跨翻译单元的顺序.
Within translation units the order of such initialization is specified. Across translation units the order is not specified.
因此,在您的情况下,由于默认情况下静态初始化为零,B::b
肯定会是 0 或 1.
So in your case, since statics will get zero initialized by default, B::b
will definitely be 0 or 1.
这篇关于如果存在依赖,编译器能否正确处理静态变量的初始化顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文