Reputation: 14129
Given the following code snippet from Anthony Williams. A very basic tuple example and everything in here works as expected.
#include <iostream>
template<typename ... Types>
class simple_tuple;
template<>
class simple_tuple<>
{};
template<typename First,typename ... Rest>
class simple_tuple<First,Rest...>:
private simple_tuple<Rest...>
{
First member;
public:
simple_tuple(First const& f,Rest const& ... rest):
simple_tuple<Rest...>(rest...),
member(f)
{}
First const& head() const
{
return member;
}
simple_tuple<Rest...> const& rest() const
{
return *this;
}
};
template<unsigned index,typename ... Types>
struct simple_tuple_entry;
template<typename First,typename ... Types>
struct simple_tuple_entry<0,First,Types...>
{
typedef First const& type;
static type value(simple_tuple<First,Types...> const& tuple)
{
return tuple.head();
}
};
template<unsigned index,typename First,typename ... Types>
struct simple_tuple_entry<index,First,Types...>
{
typedef typename simple_tuple_entry<index-1,Types...>::type type;
static type value(simple_tuple<First,Types...> const& tuple)
{
return simple_tuple_entry<index-1,Types...>::value(tuple.rest());
}
};
template<unsigned index,typename ... Types>
typename simple_tuple_entry<index,Types...>::type
get_tuple_entry(simple_tuple<Types...> const& tuple)
{
std::cout << "SizeofArgs == " << sizeof...(Types) << std::endl;
return simple_tuple_entry<index,Types...>::value(tuple);
}
int main()
{
simple_tuple<int,char,double> st(42,'a',3.141);
std::cout<<get_tuple_entry<0>(st)<<","
<<get_tuple_entry<1>(st)<<","
<<get_tuple_entry<2>(st)<<std::endl;
}
But I am wondering about the get_tuple_entry
function.
I thought that the number of variadic template parameters would vary for each call, but the sizeof always returns 3.
So the function is somehow equivalent to the following(pseudo code)
template<unsigned index, <int,char,double> >
typename simple_tuple_entry<index, <int,char,double> >::type
get_tuple_entry(simple_tuple<int,char,double> const& tuple)
{
std::cout << "SizeofArgs == " << sizeof...(<int,char,double>) << std::endl;
return simple_tuple_entry<index,<int,char,double> >::value(tuple);
}
But this would mean that get_tuple_entry
is overloaded only by the return value which is not possible. Why is the signature different for each call?
Upvotes: 1
Views: 294
Reputation: 171433
But this would mean that get_tuple_entry is overloaded only by the return value which is not possible.
get_tuple_entry
is not a function, it's a function template. What you refer to as three overloads of the same function that differ only in return type are not the same. They are distinct instantiations of the function template:
get_tuple_entry<0, int, char, double>
get_tuple_entry<1, int, char, double>
get_tuple_entry<2, int, char, double>
Which are not the same function.
I thought that the number of variadic template parameters would vary for each call, but the sizeof always returns 3
Of course. Each time you call an instantiation of that function template you pass the same argument of type simple_tuple<int,char,double>
, so each time the template parameter pack is deduced as int, char, double
which has size 3. The difference between the calls is that you call a different instantiation, and get_tuple_entry<0>
is not the same as get_tuple_entry<1>
, and each different instantiation returns a different element of the tuple.
This is really no different to
#include <iostream>
template<int N>
void func()
{
std::cout << N << '\n';
}
int main()
{
func<0>();
func<1>();
func<2>();
}
This calls three different functions that print different things, but there's no problem with identical signatures, because func<0>()
and func<1>()
and func<2>()
are all different functions. If you look at the mangled names you'll see they have different signatures, e.g. with G++ I get _Z4funcILi0EEvv
and _Z4funcILi1EEvv
and _Z4funcILi2EEvv
which are not the same signature.
Upvotes: 5