C++17 Update:
static constexpr
variables are implicitly inline
so there's no external definition necessary.
Original question:
Let's say I have a list of constants such as
struct Cls {
static constexpr int N = 32;
static constexpr int M = 64;
};
This of course suggests that I add definitions for these to avoid ODR-usage issues that may occur so I need:
constexpr int Cls::N;
constexpr int Cls::M;
Why should I prefer this over
struct Cls {
enum : int {
N = 32,
M = 64
};
};
Which saves me of the ODR-usage headaches since N
and M
are more truly just constants and not objects in their own right (a bigger deal if this is header-only) and is shorter. I could explicitly specify the type enum : long long
or whatever if need be. What is the advantage of the first?