This question is a followup question to C++17: still using enums as constants?.
Legacy constants come in several forms, notably:
#define CONSTANT xenum { CONSTANT = x };const /*int/unsigned/whatever*/ CONSTANT = x;
A comment about static constexpr and inline constexpr constants as a replacement got me thinking on the subject of updating our many, many legacy constants (particularly #define constants).
As I understand, an inline constexpr value is basically just substituted in place, like an inlined function (which I've been shown to be wrong about). Conversely, a static constexpr value is stored as part of the binary in a separate area. Assuming I understand correctly, when should one be preferred over the other? My hunch is that, for integral constants, inline constexpr will generally be preferred.
inline constexprsuitable for header files? Also theconstexpr int* z = nullptr;is more akin toint* const z = nullptr;, which may be a surprise if a person expected it to be likeint const* z = nullptr;.inlineworks wrong.inlinedoes not mean substitute the thing in the call site (although it does act as a suggestion).inlineis used for ODR purposes. see: en.cppreference.com/w/cpp/language/inlineconstexpris automatically inline. I was quoting the comment that got this started. I've updated the question to reflect that I'm really asking aboutconstexprvsstatic constexprvalues, typically integral ones.constexprdoes not implyinline.