25

cppreference states that:

A constexpr specifier used in an object declaration or non-static member function (until C++14) implies const.


Does "object declaration" mean "any variable declaration"?

I.e. is

constexpr const int someConstant = 3;

equivalent to

constexpr int someConstant = 3;

in C++11, C++14 and C++17?

Pharap
  • 3,826
  • 5
  • 37
  • 51
  • 2
    [This](https://stackoverflow.com/questions/14116003/difference-between-constexpr-and-const) holds the answer and so much more so I am hesitant to use it as a dupe. – NathanOliver May 30 '18 at 16:39
  • 3
    @NathanOliver I looked at that before, but didn't spot the part that answers my question because it's so far into the answer. That's the problem with large answers that answer multiple questions - sometimes it's hard to find the specific bit of information you're looking for. – Pharap May 30 '18 at 16:42
  • It is in the section *When can I / should I use both, const and constexpr together? A. In object declarations.* – NathanOliver May 30 '18 at 16:45

2 Answers2

26

In declarations with primitives, such as the one in your example, const is indeed redundant. However, there may be odd situations where const would be required, for example

constexpr int someConstant = 3;
constexpr const int *someConstantPointerToConstant = &someConstant;

Here, someConstantPointerToConstant is both a constexpr (i.e. it's known at compile time, hence constexpr) and it is also a pointer to constant (i.e. its object cannot be changed, hence const). The second declaration above would not compile with const omitted (demo).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 8
    the pointer itself is still implicitly const, ie you dont have to write `constexpr const int * const` because its the same as `constexpr const int *`, right? – 463035818_is_not_an_ai May 30 '18 at 17:17
  • 1
    I think so - `const` is no needed since the pointer is made in-mutable by `constexpr` already which implies its constantness; – dragonxlwang Jan 16 '21 at 19:48
  • @463035818_is_not_a_number do you know what's the point of not making pointee implicitly `const` as well? We need to write `constexpr const char *`, which seems redundant and `constexpr char *` won't even compile... – mip Dec 16 '22 at 07:49
  • @463035818_is_not_a_number but only without `constexpr`. My question is why `constexpr` have not replaced `const` entirely in constant expression contexts. This would seem reasonable to me. – mip Dec 16 '22 at 07:53
  • I’m quite puzzled by the existence and meaning of that pointer. First, `someConstant` is something that (only) lives at compile time, i.e. is used by the compiler’s C++ interpreter, may get inlined into some immediate values in instructions etc., but it is not a variable that could/should be pointed at. Second, how would such code cope with `-fPIC` where it cannot know (at compile time) the run-time address of that integer (plus there may never be such an address; as per my previous point). – Andrej Podzimek Jun 12 '23 at 11:10
5

const is redundant in const constexpr for objects.

Does "object declaration" mean "any variable declaration"?

It does.

As per cppreference, a variable or a constant is an object:

A variable is an object or a reference that is not a non-static data member, that is introduced by a declaration.

Pharap
  • 3,826
  • 5
  • 37
  • 51
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271