Iterating over tuple in C++17/20 [duplicate]

南楼画角 提交于 2019-12-05 19:38:49

Is there some way perhaps with std::apply or std::invoke ?

std::apply fit the need indeed with fold expression:

std::tuple tplb{ Test{1} , Test{2} ,  Test{3} };

std::apply([](const auto&... tests){(tests.Go(), ...);}, tplb);

Here, we call method Go() for every type value of the tuple.

The idea would be to be able to have other object types in the tuple that support the same method. If the method is present in each object then the code would compile and execute without having to use a base-class, virtuals, vtable, etc.

So above method works. It you would go further and dispatch to different implementation according to type, you might use overloaded class from std::visit's example:

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

auto f = overloaded {
        [](const Test& test) { test.Go(); },
        [](double arg) { std::cout << d << ' '; },
        [](const std::string& arg) { std::cout << arg << ' '; },
    };
std::apply([&](const auto&... e){ (overloaded(e), ...);}, my_tuple);

Bit of std::index_sequence trickery is needed to access each member of the tuple.

#include <iostream>
#include <tuple>
#include <utility>

class Test
{
    public:
        Test( int x ) : x_(x) {}; 
        void Go() const { std::cout << "Hi!" << x_ << "\n" ; } 
        int x_; 
};



template<typename F, typename T, std::size_t... Index>
void doStuffIndex(F&& action, T const& tup, std::index_sequence<Index...> const&)
{
    bool ignore[] = {((std::get<Index>(tup).*action)(), true)...};
}

template<typename F, typename... Obj>
void doStuff(F&& action, std::tuple<Obj...> const& tup)
{
    doStuffIndex(action, tup, std::make_index_sequence<sizeof...(Obj)>());
}

int main()
{
    std::tuple<Test, Test, Test> tplb{ Test{1} , Test{2} ,  Test{3} };
    doStuff(&Test::Go, tplb);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!