Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class template, which is independent of the permutations of its arguments

Consider a class template and auxiliary enum classes defined as follows:

enum class Color {Red, Green, Blue}
enum class ShowAxes {False, True}
enum class ShowLabels {False, True}

template< Color, ShowAxes, ShowLabels >
class A
{......};

The question is, how to redefine the class A, which would be independent on the permutations of its arguments. I use Dev C++, which supports C++11.

[EDIT]

For example, the new version of A should support

A< Color::Red, ShowAxes::True, ShowLabels::True >
A< Color::Red, ShowLabels::True, ShowAxes::True >
A< ShowAxes::True, Color::Red, ShowLabels::True >
A< ShowLabels::True, Color::Red, ShowAxes::True >
A< ShowLabels::True, Color::Red, ShowAxes::True >
A< ShowAxes::True, Color::Red, ShowLabels::True >

versions, and all of them are identical, i.e. they generate the same class.

like image 733
Vahagn Poghosyan Avatar asked Feb 02 '15 10:02

Vahagn Poghosyan


People also ask

What are template arguments enlist types of template arguments?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

Is vector a class template or object template?

vector is a template class, which can be instantiated with a type, in the format: vector<int> , vector<double> , vector<string> . The same template class can be used to handle many types, instead of repeatably writing codes for each of the type.

What is a template template parameter in C++?

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

What is the difference between the function template and template function?

"A function template is a template that is used to generate functions. A template function is a function that is produced by a template. For example, swap(T&, T&) is a function tem-plate, but the call swap(m, n) generates the actual template function that is invoked by the call."


1 Answers

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_constants 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;
like image 184
T.C. Avatar answered Sep 30 '22 08:09

T.C.