static const 和 constexpr 变量有什么区别? [重复]

问题描述 投票:0回答:1

我知道

constexpr
variable 可以在编译时使用。 例如,对于模板或静态断言器。

但是如果我想在没有 constexpr 的情况下做到这一点,我可以使用

static const

自 C++11/14 引入 constexpr 以来有何区别

constexpr int a = 3;
//AND
static const int a = 3;

谢谢!

查看这个问题的另一种方式是我应该使用哪个?

c++ c++11 static-variables constexpr
1个回答
10
投票

我知道的主要区别是,

constexpr
的值必须在编译时已知,而
const static
可以在运行时分配。

const static int x = rand();
© www.soinside.com 2019 - 2024. All rights reserved.