C ++ 17中的静态constexpr和静态的内联变量有什么区别? [英] What's the difference between static constexpr and static inline variables in C++17?
问题描述
使用C ++ 17,我们可以获得内联变量。
With C++17 we get inline variables.
它们的用途之一是在类中定义常量字段。
One of the uses for them is to define constant fields in classes.
那么这两个常量定义之间有什么区别:
So what's the difference between these two constant definitions:
class MyClass {
static constexpr int myFirstVar = 10;
static const inline int mySecondVar = 100;
};
当然, constexpr
使 myFirstVar
隐式内联。
在这里更好的选择是使用 constexpr
还是 inline
?
What's the better choice here, to use constexpr
or inline
?
注意:当您不需要常量性时,请 inline
使其更容易。使用 constexpr
,您没有选择。
Note: when you don't need constness, then inline
makes it easier. With constexpr
you don't have that choice.
推荐答案
您没有在声明时不必为 mySecondVar
指定一个初始化程序。初始化器本身也不必是 constexpr
。
You don't have to specify an initializer for mySecondVar
at the point of declaration. Nor is the initializer required to be constexpr
itself.
这意味着,如果我们尝试定义 myFirstVar
,例如:
This means that if we attempt to define myFirstVar
like this:
class MyClass {
static constexpr int myFirstVar;
};
int MyClass::myFirstVar = 1;
或者像这样:
#include <cstdlib>
class MyClass {
static constexpr int myFirstVar = rand();
};
这两种格式都是错误的。 constexpr
语义需要它,并且有充分的理由。
It's ill-formed either way. constexpr
semantics demand it and for a good reason.
内联
说明符方法使我们可以在标头本身中包含静态变量定义,而无需将初始化程序设为 constexpr
;或者,如果初始化程序非常复杂,则不必在类定义本身中。
The inline
specifier approach allows us to include a static variable definition in the header itself, without the initializer being constexpr
; or if the initializer is fairly complex it doesn't have to be in the class definition itself.
因此,这在C ++ 17中是一个非常有效的标头:
So this is a perfectly valid header in C++17:
#include <cstdlib>
class MyClass {
static const int mySecondVar;
};
inline const int MyClass::mySecondVar = rand();
标准向我们保证,所有包含标头的翻译单元都将看到相同的变量值,即使直到运行时我们也不知道它是什么。
The standard promises us that all translation units that include the header will see the same value for the variable, even though we won't know what it is until run-time.
它主要是一个库编写器工具。假设您的库仅是标题。然后在过去,如果需要这样定义的静态常量,您有什么选择?
It's mostly a library writers tool. Assume your library is header only. Then in the olden days, what were your options if you needed a static constant defined like this?
那么,库中可能附带了一个目标文件。它将从仅包含常量定义的转换单元进行编译。现在,该库已不再是仅标头。
Well, you could have an object file shipped with your library. It will be compiled from a translation unit that contains just the constant definition. Now the library isn't header-only.
或者您也可以依赖于内联函数。内联变量效果可以通过以下方式实现:
Or you could rely on inline functions instead. The inline variable effect can be achieved with the following:
class MyClass {
static inline int mySecondVar();
};
inline int MyClass::mySecondVar() {
static const int value = rand();
return value;
}
但是它隐藏在语法墙的后面,掩盖了本质上是一个常量,带有函数调用运算符。
But it's hidden behind a wall of syntax, and masks what is essentially a constant, with a function call operator.
这篇关于C ++ 17中的静态constexpr和静态的内联变量有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!