gpt4 book ai didi

c++ - 可变参数模板和参数推导

转载 作者:行者123 更新时间:2023-11-30 01:25:39 24 4
gpt4 key购买 nike

给定以下来自 Anthony Williams 的代码片段.一个非常基本的元组示例,这里的所有内容都按预期工作。

#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;
}

但我想知道 get_tuple_entry 函数。
我原以为可变参数模板参数的数量会因每次调用而异,但 sizeof 始终返回 3。
所以该函数在某种程度上等同于以下(伪代码)

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);
}

但这意味着 get_tuple_entry 仅由返回值重载,这是不可能的。为什么每次调用的签名都不同?

最佳答案

But this would mean that get_tuple_entry is overloaded only by the return value which is not possible.

get_tuple_entry不是一个函数,它是一个函数模板。您所说的仅在返回类型上不同的同一函数的三个重载是不相同的。它们是函数模板的不同实例:

get_tuple_entry<0, int, char, double>
get_tuple_entry<1, int, char, double>
get_tuple_entry<2, int, char, double>

这不是同一个函数。

I thought that the number of variadic template parameters would vary for each call, but the sizeof always returns 3

当然。每次调用该函数模板的实例时,您都会传递相同类型的参数 simple_tuple<int,char,double> , 所以每次推导模板参数包为int, char, double其大小为 3。调用之间的区别在于您调用了不同的实例化,并且 get_tuple_entry<0>get_tuple_entry<1> 不同,并且每个不同的实例化返回元组的不同元素。

这真的和

#include <iostream>

template<int N>
void func()
{
std::cout << N << '\n';
}

int main()
{
func<0>();
func<1>();
func<2>();
}

这调用了三个不同的函数来打印不同的东西,但是相同的签名没有问题,因为func<0>()func<1>()func<2>()都是不同的功能。如果您查看损坏的名称,您会发现它们具有不同的签名,例如使用 G++ 我得到 _Z4funcILi0EEvv_Z4funcILi1EEvv_Z4funcILi2EEvv这不是同一个签名。

关于c++ - 可变参数模板和参数推导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11953204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com