我们如何组成在C ++中返回多个值的函数 [英] How do we compose functions that return multiple values in C++
问题描述
我们如何组成在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;
}
我试图编写函数 add
和 tuple_ints
。这正确地生成错误:
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
我不想修改 add
接受一个元组;我想要的定义大体上保持原样。是否还有其他东西可以做,以便我们可以组合这两个函数?
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?
结果是有一个建议添加此功能到标准库下 N3802 。这类似于@ Jarod42提供的代码。我附加的固定代码,它使用N3802代码作为参考。大部分区别在于,提案中的代码似乎正确处理了完美的转发。
It turns out that there's a proposal to add this functionality to the standard library under N3802. This is similar to the code provided by @Jarod42. I'm attaching the fixed code, which uses the code out of N3802 for reference. Mostly, the difference is that the code in the proposal appears to handle the perfect forwarding correctly
#include <tuple>
#include <iostream>
#include <utility>
// This comes from N3802
template <typename F, typename Tuple, size_t... I>
decltype(auto) apply_impl(F&& f, Tuple&& t, std::index_sequence<I...>) {
return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
}
template <typename F, typename Tuple>
decltype(auto) apply(F&& f, Tuple&& t) {
using Indices =
std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>;
return apply_impl(std::forward<F>(f), std::forward<Tuple>(t), Indices{});
}
// Now, for our example
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 << apply(add,tuple_ints(1,2)) << std::endl;
}
此外,如果有任何混淆,此解决方案需要C ++ 14对于 std :: index_sequence
。
Also, in case there was any confusion, this solution requires C++14 for things like std::index_sequence
.
推荐答案
函数将tuple分派到参数中:
You may add a function to dispatch tuple into argument:
namespace detail
{
template <typename F, typename TUPLE, std::size_t...Is>
auto call(F f, const TUPLE& t, std::index_sequence<Is...>)
-> decltype(f(std::get<Is>(t)...))
{
return f(std::get<Is>(t)...);
}
}
template <typename F, typename TUPLE>
auto call(F f, const TUPLE& t)
-> decltype (detail::call(f, t,
std::make_index_sequence<std::tuple_size<TUPLE>::value>()))
{
return detail::call(f, t,
std::make_index_sequence<std::tuple_size<TUPLE>::value>());
}
然后这样调用
std::cout << call(add, tuple_ints(1,2)) << std::endl;
这篇关于我们如何组成在C ++中返回多个值的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!