什么是遍历 N 个变量(每个变量都是任意类型)以执行操作的简洁方法?
假设我有变量a
、b
、c
、d
、e
并希望通过所有这些执行一些操作。
最佳答案
使用 Boost.Hana 和通用 lambda:
#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;
});
}
关于c++ - 在 [现代] C++ 中通过 N 个变量进行范围/循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23235804/