3

I have some template classes whose declaration looks like this:

template <typename T, typename A, typename B, typename C>
class foo1;

template <typename T, typename A, typename B, typename C>
class foo2;

...

I use them in the following context (every foo* is instantiated with A and B and C which bar was instantiated with) :

template <typename A, typename B, typename C>
class bar {
    foo1<int, A, B, C> f1;
    foo2<int, A, B, C> f2;
    foo2<char, A, B, C> f3;
};

For simplicity and clarity reasons I would like to be able to omit A, B and C parameters inside bar and just write:

...
foo1<int> f1;
...

I know that I could just use alias template for all foo types like this:

template <typename T>
using foo1_a = foo1<T, A, B, C>;

but there could be a lot for foo types and it would require creating alias for all of them.

I tried to put all this aliases in a class:

template <typename A, typename B, typename C>
class types {
    template <typename T>
    using foo1_a = foo1<T, A, B, C>;

    ...
};

and then usage looks like this:

...
using t = types<A,B,C>;
typename t::template foo1_a<int> f1;
...

but in my opinion this looks even worse...

Is it possible to achieve this in some other way?

max66
  • 65,235
  • 10
  • 71
  • 111
Igor
  • 1,307
  • 1
  • 10
  • 19

2 Answers2

7

What about

template <template <typename...> class Cnt, typename T>
using bar = Cnt<T, A, B, C>;

used

bar<foo1, int> f1;
bar<foo2, int> f2;
bar<foo2, char> f3;

?

max66
  • 65,235
  • 10
  • 71
  • 111
2

If A B C are only supplied into bar to be forwarded into fooN parameters then it may be worth to pack them. This way there will be no need to update bar signature if fooN signatures change.

template <typename… x_FooParams>
class bar {
    foo1<int, x_FooParams...> f1;
    foo2<int, x_FooParams...> f2;
    foo2<char, x_FooParams...> f3;
};
user7860670
  • 35,849
  • 4
  • 58
  • 84