Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ typedef to fix template args

From c++11, we can do this:

template<class A, class B> class C{};
template<class A> using D = C<A, int>;

so D is C with B=int.

Is there any way to do this by typedef in c++03?

This does not work:

template <class A> typedef C<A, int> D;
like image 284
Antares Avatar asked Apr 30 '18 05:04

Antares


1 Answers

No way that is quite so straight-forward. The only things that can be templates in C++03 are classes and functions. The good thing about classes, is that they themselves can contain a typedef as a member.

template<class A>
struct D {
  typedef C<A, int> type;
};

So now D<A>::type stands for C<A, int>. This is what is known in template meta-programming as a meta-function. And it's good as you can make it in C++03.

While C++11 introduced alias templates, those require the new alias syntax, with the using keyword. An attempt with typedef, like you have, isn't valid C++11 either.

like image 120
StoryTeller - Unslander Monica Avatar answered Nov 18 '22 14:11

StoryTeller - Unslander Monica