是否可以为元组中的每种类型调用成员函数? [英] Is it possible to call a member function for every type in a tuple?

查看:77
本文介绍了是否可以为元组中的每种类型调用成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace details {
template <std::size_t I = 0, typename Tuple, typename Function, typename... Args>
typename std::enable_if<I == std::tuple_size<Tuple>::value, void>::type ForEach(Tuple &t, Function f, Args &... args) {}

template <std::size_t I = 0, typename Tuple, typename Function, typename... Args>
typename std::enable_if<(I < std::tuple_size<Tuple>::value), void>::type ForEach(Tuple &t, Function f, Args &... args) {
    f(std::get<I>(t), args...);
    ForEach<I + 1>(t, f, args...);
}
}

所有类型元组的ForEach功能的实现以上。它调用 f(tuple_type,args ...)

An implementation for ForEach functionality for all types of a tuple is above. It calls f(tuple_type, args...)

但是我想要之类的东西tuple_type.f(args ...)其中f和args是模板参数。

However I want something like tuple_type.f(args...) where f and args are template arguments.

f将是其中所有类型的成员函数

f would be a member function of all the types in the tuple, taking args... as arguments.

template <typename... Types>
class TupleManager {
    std::tuple<Types...> t;
    template <typename Function, typename... Args>
    void ForEach(Function f, Args& ... args) {
         details::ForEach<>(t, f, args...);
    }
}

说明:f必须是成员函数,即

Clarification: f needs to be a member function, i.e a function that takes the same name for all types in the tuple.

例如:

struct A {
    void foo() {
        std::cout << "A's foo\n";
    }
};
struct B : A {
    void foo() {
        std::cout << "B's foo\n";
    }
};

struct C : A {
    void foo() {
        std::cout << "C's foo\n";
    }
};

但是现在我不能通过 foo 。传递& A :: foo 打印的是A的foo。要求是在元组中为A的对象打印A'foo,在元组中为B的对象打印B'foo,在元组中为C的对象打印C的foo。

But now I can't pass foo. Passing &A::foo print's A's foo. The requirement is to print A'foo for object of A in the tuple, B's foo for object of B in the tuple, and C's foo for object of C in the tuple.

演示

推荐答案

std :: for_each 的优点在于(在范围的元素上)函数应用程序与(范围的)迭代无关。因此,不会将调用的参数传递给 std :: for_each (被调用者本身已经通过范围传递了)。考虑到这一点,您可能要考虑使用 for_each 的另一个版本,该版本更接近 std 的版本。例如,检查以下内容:在元组上迭代

The beauty of std::for_each is in its disassociation of function application (on the elements of the range) from iteration (of the range). As such, one doesn't pass the arguments of the call to std::for_each (except for the callee itself that is already passed via the range). Having that in mind, you might want to consider using a different version of for_each that is closer to the std one. Check this one for example: iterate over tuple.

如果您可以一概而论,则可以使用乔纳森·穆勒的 tuple_iterator 。这样就可以在 std :: tuple s上应用 std 算法,就像 std :: tuple std 容器。特别是,您可以使用 std :: for_each 进行所需的迭代。

If you're up to a generalisation, you may want to use Jonathan Müller's tuple_iterator. That enables application std algorithms on std::tuples just as if std::tuple were a std container. In particular, you can use std::for_each for your desirable iteration.

这篇关于是否可以为元组中的每种类型调用成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆