【发布时间】:2018-02-20 06:47:44
【问题描述】:
我正在尝试学习一些带有可变参数模板参数的 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,但我收到了关于在这种情况下缺少尾随返回类型的投诉。
有什么想法吗?
【问题讨论】:
-
“我已经尝试将返回类型设为
auto”,因为 C++11 需要尾随返回类型(即auto foo() -> returnType {/*...*/})。 C++14 允许使用简单的auto foo() {/*...*/}来推断返回类型。
标签: c++ c++11 templates variadic