I came across code like this from a programmer I highly respect:
class BigClass {
using MyId = uint32_t;
static constexpr MyId INVALID_ID() { return std::numeric_limits<MyId>::max();};
class SmallClass {
/* Constructor, etc. */
MyId id = INVALID_ID(); /* Default value */
};
};
Is there any obvious advantage to defining INVALID_ID() as a function instead of as a static const variable?
The question static constexpr variable vs function is precisely the same question I had (I used uint32_t
as an example, but I find the question interesting for other types as well). However, I am not satisfied with the answer to that question. After reading some of the answers here, I believe there are more advantages to using functions than the ability to be easily templated.