constexpr const vs constexpr variables?

22,918

Solution 1

The issue is that in a variable declaration, constexpr always applies the const-ness to the object declared; const on the other hand can apply to a different type, depending on the placement.

Thus

constexpr const int i = 3;
constexpr int i = 3;

are equivalent;

constexpr char* p = nullptr;
constexpr char* const p = nullptr;

are equivalent; both make p a const pointer to char.

constexpr const char* p = nullptr;
constexpr const char* const p = nullptr;

are equivalent. constexpr makes p a const pointer. The const in const char * makes p point to const char.

Solution 2

The error message you're seeing has nothing to do with the constexpr keyword per se.

A string literal like "foo", as in:

somefunction("foo");

The type of this string literal is const char *. The following statement:

char *const str = "foo";

This tries to assign a const char * value to a char * value. The resulting char * value is non-mutable, constant, but by that time the error already occured: an attempt to convert a const char * to a char *.

The constexpr keyword in your example is just a distraction, and has no bearing on the error.

Share:
22,918
Thomas Moulard
Author by

Thomas Moulard

Senior Software Engineer in the AWS Robotics & Automation Services team ROS 2 developer ex-Googler (Daydream, confidential robotics project)

Updated on July 08, 2022

Comments

  • Thomas Moulard
    Thomas Moulard almost 2 years

    It seems obvious that constexpr implies const and thus it is common to see:

    constexpr int foo = 42; // no const here
    

    However if you write:

    constexpr char *const str = "foo";
    

    Then GCC will spawn "warning: deprecated conversion from string constant to ‘char*’" if -Wwrite-string flag is passed.

    Writing:

    constexpr const char *const str = "foo";
    

    solves the issue.

    So are constexpr const and constexpr really the same?