Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create alias for template type?

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?

like image 740
Igor Avatar asked Feb 03 '26 23:02

Igor


1 Answers

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;

?

like image 138
max66 Avatar answered Feb 06 '26 12:02

max66