什么是C ++ 20中的"constinit"? [英] What is `constinit` in C++20?

查看:222
本文介绍了什么是C ++ 20中的"constinit"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

变量可以同时为constconstinit吗? constexprconstinit呢?

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?

该问题旨在作为一般有关constinit的后续问题的参考.

This question is intended to be used as a reference for upcoming questions about constinit in general.

推荐答案

  • constinit是什么意思?为什么要引入?在什么情况下应该使用它?
  • What does constinit mean? Why was it introduced? In which cases should we use it?

使用 静态存储期限 初始化变量可能会导致两个结果¹:

Initializing a variable with static storage duration might result in two outcomes¹:

  1. 变量在编译时初始化( 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.

  • 它使变量不可变吗?它暗示const还是constexpr?
  • Does it make a variable immutable? Does it imply const or constexpr?

不,不.

但是,constexpr确实暗示constinit.

  • 变量可以同时为constconstinit吗? constexprconstinit怎么办?
  • Can a variable be both const and constinit? What about constexpr and constinit?

它可以是constconstinit.它不能同时是constexprconstinit.措辞如下:

It can be both const and constinit. It cannot be both constexpr and constinit. From the wording:

constexprconstevalconstinit关键字中的最多一个应出现在decl-specifier-seq中.

At most one of the constexpr, consteval, and constinit keywords shall appear in a decl-specifier-seq.

constexpr不等同于const constinit,因为前者要求进行恒定销毁,而后者则不这样做.

constexpr is not equivalent to const constinit, as the former mandates constant destruction, while the latter doesn't.

  • 说明符可以应用于哪些变量?为什么我们不能将其应用于非static和非thread_local变量?
  • To which variables can the specifier be applied? Why cannot we apply it to non-static, non-thread_local variables?

它只能应用于具有静态或线程存储持续时间的变量.将其应用于其他变量没有任何意义,因为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.

  • 它具有任何性能优势吗?
  • Does it have any performance advantages?

不.但是,在编译时初始化变量的附带好处是,它不需要在程序执行期间进行初始化的指令. 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"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆