What am I allowed to do with a static, constexpr, in-class initialized data member?

Does it mean that a static constexpr variable is not odr-used (and
therefore can be in-class initialized) if it is returned from a
function?

Yes.

Essentially, as long as you treat it as a value, rather than an object, then it is not odr-used. Consider that if you pasted in the value, the code would function identically- this is when it is treated as an rvalue. But there are some scenarios where it would not.

There are only a few scenarios where lvalue-to-rvalue conversion is not performed on primitives, and that’s reference binding, &obj, and probably a couple others, but it’s very few. Remember that, if the compiler gives you a const int& referring to period, then you must be able to take it’s address, and furthermore, this address must be the same for each TU. That means, in C++’s horrendous TU system, that there must be one explicit definition.

If it is not odr-used, the compiler can make a copy in each TU, or substitute the value, or whatever it wants, and you can’t observe the difference.

Leave a Comment