It isn't possible with your current interface using non-type parameters.
You can take type parameters instead and wrap the values in a 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;
This is rather verbose, so you could consider writing a macro:
#define AS_IC(Value) std::integral_constant<decltype(Value), Value>
and rewrite as
A<AS_IC(Color::Red), AS_IC(ShowAxes::True), AS_IC(ShowLabels::True)> a;
Extracting the value of the desired type from the list of integral_constant
s is straightforward:
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...> {};
Then you can do
// inside the definition of A
static constexpr Color col = extract<Color, X, Y, Z>::value;
Demo.
This do not, however, generate the same class, but you can make a class template A_impl
that behaves like your A
with non-type parameters, and that contains the actual implementation, and then make A
an alias template:
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>;
Now given
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
and b
have the same type. Demo.
In the alternative, you can also use decltype
and overloading function templates, but that requires adding a function template declaration for every possible order of types:
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;