对一个对象上的每个元组元素调用函数,不进行递归
Call function for each tuple element on one object without recursion
我有一个类A
的对象,它可以用不同的类型调用,并且在每次调用时返回改变的self。对于这个问题,A
将做
struct A {
A call(const int&) {
}
A call(const string& s) {
}
////
} a;
我有一个未知类型的元组
std::tuple<Types...> t;
,我想对每个元组元素调用a
,所以我想得到这样的东西:
b = a;
b = b.call(get<0>(t));
b = b.call(get<1>(t));
b = b.call(get<2>(t));
//...
或
b = a.call(get<0>(t)).call(get<1>(t)).call(get<2>(t)...)
顺序不是很重要(我的意思是如果调用顺序颠倒甚至打乱它是可以的)。
我确实理解递归是可能的,但它相当丑陋。有没有可能实现没有递归?
您可以使用std::index_sequence<Is...>
,例如:
namespace detail
{
template <std::size_t...Is, typename T>
void a_call(A& a, std::index_sequence<Is...>, const T& t)
{
int dummy[] = {0, ((a = a.call(std::get<Is>(t))), void(), 0)...};
static_cast<void>(dummy); // Avoid warning for unused variable.
}
}
template <typename ... Ts>
void a_call(A& a, const std::tuple<Ts...>& t)
{
detail::a_call(a, std::index_sequence_for<Ts...>{}, t);
}
在c++ 17中,折叠表达式允许:
template <std::size_t...Is, typename T>
void a_call(A& a, std::index_sequence<Is...>, const T& t)
{
(static_cast<void>(a = a.call(std::get<Is>(t))), ...);
}
或甚至,与std::apply
:
template <typename ... Ts>
void a_call(A& a, const std::tuple<Ts...>& t)
{
std::apply([&](const auto&... args){ (static_cast<void>(a = a.call(args)), ...); }, t);
}
可以使用boost。融合迭代元组
http://theboostcpplibraries.com/boost.fusion使用"get part of std::tuple":
#include <iostream>
#include <string>
#include <algorithm>
#include <tuple>
//SEE https://stackoverflow.com/questions/8569567/get-part-of-stdtuple
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
template <size_t... n>
struct ct_integers_list {
template <size_t m>
struct push_back
{
typedef ct_integers_list<n..., m> type;
};
};
template <size_t max>
struct ct_iota_1
{
typedef typename ct_iota_1<max - 1>::type::template push_back<max>::type type;
};
template <>
struct ct_iota_1<0>
{
typedef ct_integers_list<> type;
};
template <size_t... indices, typename Tuple>
auto tuple_subset(const Tuple& tpl, ct_integers_list<indices...>)
-> decltype(std::make_tuple(std::get<indices>(tpl)...))
{
return std::make_tuple(std::get<indices>(tpl)...);
// this means:
// make_tuple(get<indices[0]>(tpl), get<indices[1]>(tpl), ...)
}
template <typename Head, typename... Tail>
std::tuple<Tail...> tuple_tail(const std::tuple<Head, Tail...>& tpl)
{
return tuple_subset(tpl, typename ct_iota_1<sizeof...(Tail)>::type());
// this means:
// tuple_subset<1, 2, 3, ..., sizeof...(Tail)-1>(tpl, ..)
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//SEE https://stackoverflow.com/questions/8569567/get-part-of-stdtuple
struct A
{
A call(const int&) {
std::cout << "int" << std::endl;
return *this;
}
A call(const std::string& s) {
std::cout << "string" << std::endl;
return *this;
}
template <typename T>
A call(std::tuple<T> tpl)
{
return call(std::get<0>(tpl));
}
template <typename T, typename...Types>
A call(std::tuple<T, Types...> tpl)
{
return call(std::get<0>(tpl)).call(tuple_tail(tpl));
}
} a;
int main()
{
std::tuple<int, std::string, int, int, std::string> t(0, "1", 2, 3, "4");
A b = a.call(t);
}
输出:int
string
int
int
string
使用std::integer_sequence和std::make_integer_sequence如下所示:
struct A {
A call(const int&) {
std::cout << "Calling A::call(int)" << std::endl;
return A{};
}
A call(const std::string& s) {
std::cout << "Calling A::call(std::string)" << std::endl;
return A{};
}
////
} a;
template<typename Sequence>
struct call_helper;
template<std::size_t Current, std::size_t... Rest>
struct call_helper<std::integer_sequence<std::size_t, Current, Rest...>>
{
template<typename Tuple, typename Result>
static auto call(Tuple const& tup, Result&& res)
{
// Call the next helper with the rest sequence
return call_helper<
std::integer_sequence<std::size_t, Rest...>
>::call(tup, res.call(std::get<Current>(tup)));
}
};
template<>
struct call_helper<std::integer_sequence<std::size_t>>
{
// End reached, just return the value
template<typename Tuple, typename Result>
static auto call(Tuple const&, Result&& res)
-> std::decay_t<Result>
{
return std::forward<Result>(res);
}
};
template<typename Tuple, typename Result>
auto call_all(Tuple const& tup, Result res)
{
return call_helper<std::make_integer_sequence<
std::size_t, std::tuple_size<Tuple>::value>
>::call(tup, std::forward<Result>(res));
}
// Test call:
std::tuple<int, std::string, int, std::string, std::string> mytup;
call_all(mytup, a);
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 如何在Elixir中调用递归函数并行
- 递归函数有效,但无法记忆
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- 为什么我的递归函数按降序打印,然后按升序打印?
- 为什么递归函数的最终输出是 5?
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 如何将记忆应用于此递归函数?
- 如何从递归函数中完全返回,该函数给出了每个函数结果的累积相加?
- 无穷大而循环时具有递归函数
- 即使没有调用这个递归函数,它是如何工作的?
- 如何使此递归函数从给定的起始位置返回最小的整数?
- 复制构造函数中的递归调用
- 为什么编译器将其解析为函数指针而不是递归调用?
- 此递归函数的每次迭代的值存储在哪里?
- 在 C++14 中使用通用 lambda 函数递归
- 大量函数递归-C++
- 用内部类函数递归地添加数组数
- 函数递归 C++ 的最大数量