Reputation: 69988
If I have,
template<typename T1, typename T2, int N>
class X {};
Is there any way, that I can know class X
has 3 template
arguments ?
Use case in brief: There are two library classes ptr<T>
(for normal pointer) and ptr_arr<T,N>
(for pointer to array). These two are interacting with another class in following way:
template<typename T>
void Clear(const T &obj)
{
if(T::Args == 1) destroy(obj);
else destroy_arr(obj);
}
So, I thought if we have some handy way of knowing the number of parameters, it would make it easy. However, I learn that I need to change my business logic as there cannot be such way.
Upvotes: 3
Views: 154
Reputation: 506877
There is no way to do this. Imagine the amount of overloads.
template<int> struct A;
template<bool> struct B;
template<char> struct C;
template<typename> struct D;
template<D<int>*> struct E;
template<D<bool>*> struct F;
template<typename, int> struct G;
// ...
For each of that, you would need a different template to accept them. You cannot even use C++0x's variadic templates, because template parameter packs only work on one parameter form and type (for example, int...
only works for a parameter pack full of integers).
Upvotes: 0
Reputation: 33395
There is no standard way to do this (unless you use variadic sizeof(Args...)
in C++0x) but that's beside the point -- the question is wrong.
Use overload resolution.
template <typename T>
void clear (ptr<T> & obj) {
destroy (obj);
}
template <typename T, int N>
void clear (ptr_arr<T,N> & obj) {
destroy_arr (obj);
}
Upvotes: 6
Reputation: 51445
You can use the mpl::template_arity
(undocumented)
http://www.boost.org/doc/libs/1_40_0/boost/mpl/aux_/template_arity.hpp
Upvotes: 5