I have a template Conditional
which returns value
according to the boolean expression:
template<bool C, typename C1, typename C2>
struct Conditional {
};
template<typename C1, typename C2>
struct Conditional<true, C1, C2> {
typedef C1 value;
};
template<typename C1, typename C2>
struct Conditional<false, C1, C2> {
typedef C2 value;
};
<Conditional<(0 != 1), Int<0>, Int<1>>::value; // Int<0>
<Conditional<(0 == 1), Int<0>, Int<1>>::value, // Int<1>
I want to make a ConditionalInteger
template where his behavior derives from Condition
ConditionalInteger<(0 != 1), 0, 1>::value == 0; // true
ConditionalInteger<(0 == 1), 0, 1>::value == 1 // false
With straight forward approach it works:
template<bool C, int N1, int N2>
struct ConditionalInteger {
};
template<int N1, int N2>
struct ConditionalInteger<true,N1,N2> {
static constexpr int value = N1;
};
template<int N1, int N2>
struct ConditionalInteger<false,N1,N2> {
static constexpr int value = N2;
};
How do I implement it using Conditional
?
With the next try, I get an error:
No type named ‘value’ in ‘struct IntWrapper<0>’
template<int N>
struct IntWrapper {
static constexpr int value = N;
};
template<bool C, int N1, int N2>
struct ConditionalInteger {
using value = typename Conditional<C, typename IntWrapper<N1>::value, typename IntWrapper<N2>::value>::value;
};