C++ 特定元组元素之和
我试图计算元组中特定元素的总和,但无法将其编译 下面是我想要编写的函数的样子:C++ 特定元组元素之和,c++,variadic-templates,C++,Variadic Templates,我试图计算元组中特定元素的总和,但无法将其编译 下面是我想要编写的函数的样子: template <int ...ids> float sum(std::tuple<int, int, std::string, float> things) { return "<sum of elements of ids>"; } 模板 浮点和(std::tuple things) { 返回“”; } 我想这样称呼它: std::tuple&
template <int ...ids>
float sum(std::tuple<int, int, std::string, float> things)
{
return "<sum of elements of ids>";
}
模板
浮点和(std::tuple things)
{
返回“”;
}
我想这样称呼它:
std::tuple<int, int, std::string, float> my_things= { 1, 2, "3", 4.0f };
float sum_numbers = sum<0, 1, 3>(my_things);
std::tuple my_things={1,2,3,4.0f};
浮点数=总和(我的东西);
我无法使用折叠使其工作。:/这可能吗?若有,原因为何
提前谢谢
我无法使用折叠使其工作。:/这可能吗?若有,原因为何
下面呢
template <int ...ids>
float sum (std::tuple<int, int, std::string, float> things)
{
return ( 0.0f + ... + std::get<ids>(things) );
}
template <std::size_t ... Ids, template T>
auto sum (T const & things)
{ return ( 0 + ... + std::get<Ids>(things) ); }