Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 512
embedc Avatar asked May 20 '19 10:05

embedc


1 Answers

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?

like image 128
Max Langhof Avatar answered Sep 20 '22 11:09

Max Langhof