Why is the address of a constexpr local variable not a constant expression?

The following constexpr function fails to compile:

constexpr void fnc()
{
    constexpr int i = 5;
    constexpr auto ptr = &i;
}

Why can’t ptr be constexpr, considering that all the evaluations take place in a constexpr function and it works without declaring ptr constexpr?

>Solution :

The error message from clang is clear enough:

address of non-static constexpr variable ‘i’ may differ on each invocation of the enclosing function; add ‘static’ to give it a constant address

Leave a Reply