У меня есть шаблон Conditional
, который возвращает value
в соответствии с логическим выражением:
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>
Я хочу создать шаблон ConditionalInteger
, в котором его поведение основано на Condition
ConditionalInteger<(0 != 1), 0, 1>::value == 0; // true
ConditionalInteger<(0 == 1), 0, 1>::value == 1 // false
При прямолинейном подходе это работает:
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;
};
Как мне реализовать это, используя Conditional
?
При следующей попытке я получаю сообщение об ошибке:
Нет типа с именем "value" в "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;
};