为什么 constexpr 应该是静态的?

Why constexpr should be static?

看完this and this还是对这种表达方式感到困惑:

static constexpr int = 0;

据我所知,在 C++ 中:

我觉得他们很矛盾。 static 确保变量将具有长期内存地址,而 constexpr 确保相反的假设。令人惊讶的是,第一个 link 中的讨论提到了这一点:

constexpr int x = 3;
const int* p = &x;

如果x是一个右值,我们怎么才能得到它的内存地址呢?

谁能解释一下?

static 有多种含义。在 classes 中(根据您的评论),这意味着成员与 class 相关联,而不是 class.

的特定实例(对象)

对于 constexpr,这很有意义。这通常由编译器已知的值初始化,而不是来自 ctor 参数。