I'm trying to understand how to create and call variadic templates with function, inpus args and output args types. I wrote this toy example:
#include <tuple>
template<typename Func, typename... Inputs, typename... Outputs>
std::tuple<double, Outputs...> foo(int init, Func&& func, Inputs&&... args) {
return std::forward<Func>(func)(init, std::forward<Inputs>(args)...);
};
int main () {
int init = 6;
double mult = 2.3;
std::tuple<double, double> bar = foo(
init,
[](int init_, double mult_) {
double res = init_ * mult_;
return std::make_tuple(res, 4.1);
},
mult
);
int out = std::get<0>(bar);
return out;
}
However, it doesn't compile. How should I modify it to get 13 as result?
I get this error message:
<source>: In function 'int main()':
<source>:11:41: error: conversion from 'tuple<double>' to non-scalar type 'tuple<double, double>' requested
11 | std::tuple<double, double> bar = foo(
| ~~~^
12 | init,
| ~~~~~
13 | [](int init_, double mult_) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14 | double res = init_ * mult_;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 | return std::make_tuple(res, 4.1);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16 | },
| ~~
17 | mult
| ~~~~
18 | );
| ~
<source>: In instantiation of 'std::tuple<double, Outputs ...> foo(int, Func&&, Inputs&& ...) [with Func = main()::<lambda(int, double)>; Inputs = {double&}; Outputs = {}]':
<source>:18:5: required from here
<source>:5:72: error: could not convert 'main()::<lambda(int, double)>(init, std::forward<double&>((* & args#0)))' from 'tuple<double, double>' to 'tuple<double>'
5 | return std::forward<Func>(func)(init, std::forward<Inputs>(args)...);
| ^
| |
| tuple<double, double>
Aucun commentaire:
Enregistrer un commentaire