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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With