是否可以声明一个 constexpr 指针而不是指向 constexpr 的指针?

Is it possible to declare a constexpr pointer instead of a pointer to constexpr?

这样说对吗

constexpr int* p = nullptr;

声明 constexpr 指针(而不是指向 constexpr int 的指针)?

这个定义

int* constexpr p = nullptr;

给出编译错误。

标准在 [dcl.constexpr]/1 中说只有变量或函数(及其模板)可以是 constexpr:

The constexpr specifier shall be applied only to the definition of a variable or variable template or the declaration of a function or function template.

特别是,constexpr 不是类型系统的一部分(尽管它确实暗示了 const),因此 pointer-to-constexpr 在 C++ 中不是一个有意义的概念。

将变量标记为 constexpr 的主要原因是使它们成为 usable in constant expressions。但是,也可以使用引用和常量整数值,它们在常量表达式中用常量初始值设定项进行初始化。也许参考部分可以帮助您解决您正在处理的任何问题?