我正在尝试学习一些带有可变参数模板参数的c ++ 11。
我想将一个浮点输入参数列表转换为convertTest(),然后返回一个std :: tuple的整数。我尝试在g ++中编译以下内容:
template<typename ...ArgsIn, typename ...ArgsOut>
static inline std::tuple<ArgsOut...> convertTest(float nextArg, ArgsIn... remainingArgs)
{
auto a = convertTest(remainingArgs...);
auto b = std::make_tuple(int(nextArg));
auto c = std::tuple_cat(a, b);
return c;
}
static inline std::tuple<int> convertTest(float lastArgIn)
{
return std::make_tuple((int)lastArgIn);
}
int main()
{
auto res = convertTest(0.5f, 10.11f);
return 0;
}
我收到以下错误:
error: conversion from 'std::tuple<int, int>' to non-scalar type 'std::tuple<>' requested
我不确定为什么返回类型std::tuple<ArgsOut...>
会解析为std::tuple<>
。有什么想法吗?
我已尝试制作返回类型auto
,但在这种情况下,我收到有关缺少尾随返回类型的投诉。
有什么想法吗?
答案 0 :(得分:4)
Argout
不可扣除,因此成为空列表。
所以你必须按顺序编写函数
template<typename ... ArgsOut, typename ...ArgsIn>
static std::tuple<ArgsOut...> convertTest(float nextArg, ArgsIn... remainingArgs);
并称之为
convertTest<int, int>(0.5f, 10.11f);
顺便说一下,你可以简单地把它写成(删除你独占的事实float
)
template<typename ...Args>
auto convertTest(Args... args)
-> decltype(std::make_tuple(static_cast<int>(args)...))
{
return std::make_tuple(static_cast<int>(args)...)
}