静态constexpr变量是否有意义? [英] Does static constexpr variable make sense?

查看:195
本文介绍了静态constexpr变量是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在一个函数里面有一个变量(比如说一个大数组),那么有必要声明它 static constexpr constexpr 保证数组在编译时创建,因此 static 是无用的?

If I have a variable inside a function (say, a large array), does it make sense to declare it both static and constexpr? constexpr guarantees that the array is created at compile time, so would the static be useless?

void f() {
    static constexpr int x [] = {
        // a few thousand elements
    };
    // do something with the array
}

推荐答案

答案是,不仅 static 很有用,总是很好用。

The short answer is that not only is static useful, it is pretty well always going to be desired.

首先,注意 static constexpr 完全独立于彼此。 static 定义对象在执行期间的生命周期; constexpr 指定对象在编译期间应该可用。编译和执行在时间和空间上都是不相交和不连续的。因此,一旦程序被编译, constexpr 就不再相关。

First, note that static and constexpr are completely independent of each other. static defines the object's lifetime during execution; constexpr specifies that the object should be available during compilation. Compilation and execution are disjoint and discontiguous, both in time and space. So once the program is compiled, constexpr is no longer relevant.

每个变量声明 constexpr 是隐式的 const const static 几乎是正交的(除了与 static const 整数的交互。)

Every variable declared constexpr is implicitly const but const and static are almost orthogonal (except for the interaction with static const integers.)

$ c> C ++ 对象模型(§ 1.9)要求除了位域以外的所有对象占用至少一个字节的内存和地址;此外,在给定时刻在程序中可观察到的所有这样的对象必须具有不同的地址(第6段)。这不需要编译器在每次调用具有局部非静态const数组的函数时在栈上创建一个新数组,因为编译器可以避免 as-if

The C++ object model (§1.9) requires that all objects other than bit-fields occupy at least one byte of memory and have addresses; furthermore all such objects observable in a program at a given moment must have distinct addresses (paragraph 6). This does not quite require the compiler to create a new array on the stack for every invocation of a function with a local non-static const array, because the compiler could take refuge in the as-if principle provided it can prove that no other such object can be observed.

这不是很容易证明,不幸的是,除非该函数是琐碎的例如,它不调用其主体在翻译单元内不可见的任何其他函数),因为根据定义,或多或少的数组是地址。因此,在大多数情况下,非静态的 const(expr)数组将不得不在每次调用的堆栈上重新创建,这违反了能够计算它的点编译时间。

That's not going to be easy to prove, unfortunately, unless the function is trivial (for example, it does not call any other function whose body is not visible within the translation unit) because arrays, more or less by definition, are addresses. So in most cases, the non-static const(expr) array will have to be recreated on the stack at every invocation, which defeats the point of being able to compute it at compile time.

另一方面,一个局部 static const 对象由所有观察者共享,即使它定义的函数从未被调用也被初始化。所以上面没有一个适用,并且编译器自由不仅仅生成它的一个实例;

On the other hand, a local static const object is shared by all observers, and furthermore may be initialized even if the function it is defined in is never called. So none of the above applies, and a compiler is free not only to generate only a single instance of it; it is free to generate a single instance of it in read-only storage.

所以你应该肯定使用 static constexpr

但是,有一种情况你不想使用 static constexper 。除非 constexpr 声明的对象是ODR使用或声明为 static ,编译器可以不包括它。这是非常有用的,因为它允许使用编译时临时 constexpr 数组,而不会以不必要的字节污染编译的程序。在这种情况下,你显然不想使用 static ,因为 static 很可能迫使对象存在运行时。

However, there is one case where you wouldn't want to use static constexper. Unless a constexpr declared object is either ODR-used or declared static, the compiler is free to not include it at all. That's pretty useful, because it allows the use of compile-time temporary constexpr arrays without polluting the compiled program with unnecessary bytes. In that case, you would clearly not want to use static, since static is likely to force the object to exist at runtime.

这篇关于静态constexpr变量是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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