Is it correct that
constexpr int* p = nullptr;
declares constexpr pointer (instead of a pointer to constexpr int)?
This definition
int* constexpr p = nullptr;
gives a compilation error.
The standard says in [dcl.constexpr]/1 that only variables or functions (and their templates) can be 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.
In particular, constexpr
is not part of the type system (although it does imply const
), so pointer-to-constexpr is not a meaningful concept in C++.
The primary reason to mark variables as constexpr
is to make them usable in constant expressions. However, one can also use references and constant integral values which are initialized with constant initializers within constant expressions. Maybe the reference part could help you solve whatever problem you are dealing with?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With