mardi 6 janvier 2015

How do we compose functions that return multiple values in C++

How do we compose functions that return multiple return values in C++? More specifically, if one function returns a tuple, can we compose this function with another that does not explicitly accept tuples? For example, in the code:



#include <tuple>
#include <iostream>

std::tuple <int,int> tuple_ints(int x,int y) {
return std::tuple <int,int> (x,y);
}

int add(int x,int y) {
return x+y;
}

int main() {
std::cout << add(tuple_ints(1,2)) << std::endl;
}


I'm trying to compose the functions add and tuple_ints. This rightly generates the error:



g++ -std=c++11 test01.cpp -o test01
test01.cpp: In function 'int main()':
test01.cpp:17:37: error: cannot convert 'std::tuple<int, int>' to 'int' for argument '1' to 'int add(int, int)'
std::cout << add(tuple_ints(1,2)) << std::endl;
^
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1


I don't want to modify add to accept a tuple; I want the definition to stay largely as it is. Is there something else that we can do so that we can compose these two functions?


Aucun commentaire:

Enregistrer un commentaire