[英]operate on c++ 11 variadic template parameters, store into tuple
我正在嘗試使用可變參數模板參數學習一些c ++ 11。
我想將一個浮點輸入參數列表轉換為convertTest(),然后返回一個int的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<>
。 有任何想法嗎?
我曾嘗試將return類型設置為auto
,但是在這種情況下,我抱怨缺少尾隨返回類型。
有任何想法嗎?
Argout
是不可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)...)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.