Currently using g++11.3.0, C++20.
I'm trying to compile the code below, where the function foo
takes an arbitrary number of template arguments and returns a tuple that contains a value for each template argument. The code compiles and runs for me when I use the statements foo<int>()
and foo<float>()
, but runs into an error when I use the statement foo<int, float>()
:
error: no viable conversion from returned value of type 'tuple<typename __unwrap_ref_decay<float>::type, (no argument)>' to function return type 'tuple<int, float>'
However, if I change the return type from std::tuple<Args...>
to auto
, it compiles and runs for all three statements. Is return type deduction the only way to go or is there a way to avoid using the auto
keyword?
template <typename ...Args>
std::tuple<Args...> foo()
{
return std::make_tuple(([] ()
{
// Do some stuff....
// Return value....
return Args();
}(), ...));
}
You use the syntax for folding expression using comma operator, instead you need:
template <typename ...Args>
std::tuple<Args...> foo()
{
return std::make_tuple([] ()
{
// Do some stuff....
// Return value....
return Args();
}()...);
}