简体   繁体   中英

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM