This is maybe a basic question, but I cannot see the response by myself right now.
Consider the following code:
template<bool b>
struct T {
static constexpr int value = (b ? 42 : 0);
};
template<bool b>
struct U {
enum { value = (b ? 42 : 0) };
};
int main() {
static_assert(T<true>::value == 42, "!");
static_assert(T<false>::value == 0, "!");
static_assert(U<true>::value == 42, "!");
static_assert(U<false>::value == 0, "!");
}
I'm used to using structs like T
, but more than once I've seen structs like U
used for the same purpose (mostly traits definition).
As far as I can see, they are both resolved at compile time and they solve almost the same problem, but it seems to me that T
is far more readable than U
(well, I know, my personal opinion).
My question is pretty simple: is there any technical reason for which one solution is better than the other one?
Even more, is there any case for which one of them is not a viable solution?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…