问题描述
constinit
是新的关键字和说明符. org/jtc1/sc22/wg21/docs/papers/2019/p1143r2.html"rel =" noreferrer> P1143 .
constinit
is a new keyword and specifier in C++20 which was proposed in P1143.
标准中提供了以下示例:
The following example is provided in the standard:
const char * g() { return "dynamic initialization"; }
constexpr const char * f(bool p) { return p ? "constant initializer" : g(); }
constinit const char * c = f(true); // OK
constinit const char * d = f(false); // ill-formed
我想到了几个问题:
-
constinit
是什么意思?为什么要引入?在什么情况下应该使用它?
What does
constinit
mean? Why was it introduced? In which cases should we use it?
它使变量不可变吗?它暗示const
还是constexpr
?
Does it make a variable immutable? Does it imply const
or constexpr
?
变量可以同时为const
和constinit
吗? constexpr
和constinit
呢?
Can a variable be both const
and constinit
? What about constexpr
and constinit
?
说明符可以应用于哪些变量?为什么我们不能将其应用于非static
和非thread_local
变量?
To which variables can the specifier be applied? Why cannot we apply it to non-static
, non-thread_local
variables?
它具有任何性能优势吗?
Does it have any performance advantages?
推荐答案
使用 静态存储期限 初始化变量可能会导致两个结果¹:
Initializing a variable with static storage duration might result in two outcomes¹:
-
变量在编译时初始化( constant-initialization );
该变量在控件第一次通过其声明时进行初始化.
The variable is initialized the first time control passes through its declaration.
案例(2)有问题,因为它可能导致 static初始化顺序惨败 ,它是与全局对象相关的危险错误的来源.
Case (2) is problematic because it can lead to the static initialization order fiasco, which is a source of dangerous bugs related to global objects.
constinit
关键字只能应用于具有静态存储持续时间的变量.如果在编译时未初始化修饰的变量,则程序格式错误(即不编译).
The constinit
keyword can only be applied on variables with static storage duration. If the decorated variable is not initialized at compile-time, the program is ill-formed (i.e. does not compile).
使用constinit
可确保在编译时初始化变量,并且不会发生静态初始化顺序失败.
Using constinit
ensures that the variable is initialized at compile-time, and that the static initialization order fiasco cannot take place.
不,不.
但是,constexpr
确实暗示constinit
.
它可以是const
和constinit
.它不能同时是constexpr
和constinit
.措辞如下:
It can be both const
and constinit
. It cannot be both constexpr
and constinit
. From the wording:
constexpr
不等同于const constinit
,因为前者要求进行恒定销毁,而后者则不这样做.
constexpr
is not equivalent to const constinit
, as the former mandates constant destruction, while the latter doesn't.
它只能应用于具有静态或线程存储持续时间的变量.将其应用于其他变量没有任何意义,因为constinit
都是关于静态初始化的.
It can only be applied to variables with static or thread storage duration. It does not make sense to apply it to other variables, as constinit
is all about static initialization.
不.但是,在编译时初始化变量的附带好处是,它不需要在程序执行期间进行初始化的指令. constinit
帮助开发人员确保确实如此,而不必猜测或检查生成的程序集.
No. However, a collateral benefit of initializing a variable at compile-time is that it doesn't take instructions to initialize during program execution. constinit
helps developers ensure that is the case without having to guess or check the generated assembly.
¹:请参见 https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables
这篇关于什么是C ++ 20中的"constinit"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!