Это невозможно с вашим текущим интерфейсом, использующим нетиповые параметры.
Вместо этого вы можете взять параметры типа и обернуть значения в std::integral_constant
:
template<class X, class Y, class Z>
class A { /* stuff */ };
// use as:
A<std::integral_constant<Color, Color::Red>,
std::integral_constant<ShowAxes, ShowAxes::True>,
std::integral_constant<ShowLabels, ShowLabels::True>> a;
Это довольно многословно, поэтому вы можете написать макрос:
#define AS_IC(Value) std::integral_constant<decltype(Value), Value>
и переписать как
A<AS_IC(Color::Red), AS_IC(ShowAxes::True), AS_IC(ShowLabels::True)> a;
Извлечь значение нужного типа из списка integral_constant
s очень просто:
template<class Result, class...>
struct extract;
template<class Result, Result Value, class... Tail>
struct extract<Result, std::integral_constant<Result, Value>, Tail...> : std::integral_constant<Result, Value> {};
template<class Result, class Head, class... Tail>
struct extract<Result, Head, Tail...> : extract<Result, Tail...> {};
Тогда вы можете сделать
// inside the definition of A
static constexpr Color col = extract<Color, X, Y, Z>::value;
Демо.
Это, однако, не создает тот же класс, но вы можете создать шаблон класса A_impl
, который ведет себя как ваш A
с нетиповыми параметрами и содержит реальную реализацию, а затем сделать A
шаблоном псевдонима:
template< Color, ShowAxes, ShowLabels >
class A_impl
{/* stuff */};
template<class X, class Y, class Z>
using A = A_impl<extract<Color, X, Y, Z>::value,
extract<ShowAxes, X, Y, Z>::value,
extract<ShowLabels, X, Y, Z>::value>;
Теперь дано
A<AS_IC(Color::Red), AS_IC(ShowAxes::True), AS_IC(ShowLabels::True)> a;
A<AS_IC(Color::Red), AS_IC(ShowLabels::True), AS_IC(ShowAxes::True)> b;
a
и b
имеют один и тот же тип. Демо.
В качестве альтернативы вы также можете использовать decltype
и шаблоны функций перегрузки, но это требует добавления объявления шаблона функции для каждого возможного порядка типов:
template< Color c, ShowAxes a, ShowLabels l>
A<c,a,l> A_of();
template< ShowAxes a, ShowLabels l, Color c>
A<c,a,l> A_of();
// etc.
decltype(A_of<Color::Red, ShowAxes::True, ShowLabels::True>()) a1;
decltype(A_of<ShowAxes::True, ShowLabels::True, Color::Red>()) a2;
person
T.C.
schedule
02.02.2015
std::integral_constant
, но он довольно быстро становится многословным. - person T.C.   schedule 02.02.2015