簡體   English   中英

在沒有遞歸的情況下為一個對象上的每個元組元素調用函數

[英]Call function for each tuple element on one object without recursion

我有一個A類的對象,可以用不同的類型調用,並在每次調用時返回self。 出於這個問題A目的, 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中,Folding表達式允許:

    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);
}

在“ 獲取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);

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM