范围/循环通过N变量在[现代] C + + [英] Range/Loop through N variables in [modern] C++
本文介绍了范围/循环通过N变量在[现代] C + +的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有变量一个简单的方法,通过N个变量,任何类型的每个变量,执行一个操作?
, b
, c
, d $ c $
What's a succinct way of ranging through N variables, of any type each, to perform an operation?
推荐答案
使用Boost.Fusion和通用lambdas:
Use Boost.Fusion and generic lambdas:
#include <tuple>
#include <iostream>
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
struct A {};
struct B {};
struct C {};
struct D {};
struct E {};
int main() {
using namespace std;
using namespace boost::fusion;
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;
});
}
http://coliru.stacked-crooked.com/a/5d84afd0ebe7fd5d
这篇关于范围/循环通过N变量在[现代] C + +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文