在[modern] C++中通过N个变量进行范围/循环

pep*_*ico 2 c++ boost c++11 c++14 c++17

通过各种类型的N个变量来执行操作的简洁方法是什么?

比方说,我有变量a,b,c,d,e并想通过所有的人进行一些操作.

pep*_*ico 6

使用Boost.Hana和通用lambdas:

#include <tuple>
#include <iostream>
#include <boost/hana.hpp>
#include <boost/hana/ext/std/tuple.hpp>

struct A {};
struct B {};
struct C {};
struct D {};
struct E {};

int main() {
    using namespace std;
    using boost::hana::for_each;

    A a;
    B b;
    C c;
    D d;
    E e;

    for_each(tie(a, b, c, d, e), [](auto &x) {
        cout << typeid(x).name() << endl;
    });
}
Run Code Online (Sandbox Code Playgroud)

http://coliru.stacked-crooked.com/a/ccb37ec1e453c9b4