I have a tuple
style variadic template data type. I want to get tuple_size
type function that gets the number of template parameters. However, I'm having trouble getting the following to work.
template<typename... T>
struct A
{
};
template<typename T, typename ...S>
struct A<T, S...>
{
T t;
A<S...> s;
};
template<typename T>
size_t size_A()
{
return 1;
}
template<typename T, typename... S>
size_t size_A()
{
return 1 + size_A<S...>();
}
int main() {
A<int,double,uint> a {2,3.5,10};
constexpr size_t n = size_A<decltype(a)>();
std::cout << n << std::endl;
}
I get the following error:
error: call of overloaded ‘size_A<A<int, double, unsigned int> >()’ is ambiguous
constexpr size_t n = size_A<decltype(a)>();
note: candidate: ‘size_t size_A() [with T = A<int, double, unsigned int>; size_t = long unsigned int]’
size_t size_A()
^~~~~~
note: candidate: ‘size_t size_A() [with T = A<int, double, unsigned int>; S = {}; size_t = long unsigned int]’
size_t size_A()
^~~~~~
How do I make it distinguish between having parameter packs and not having them?
The following does work but I want to just use the type and not have to find an instance of the type. The actual problem will have the type and maybe no instance of the type.
template<typename T>
constexpr size_t size(const A<T>& t)
{
return 1;
}
template<typename T, typename... S>
constexpr size_t size(const A<T,S...>& t)
{
return 1 + size(t.s);
}
This differs from the varidic template functions where the instance of the type is given.
How do I make size_A
work like tuple_size
so that I can get the number of template parameters just by the type?