Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading