Vote count:
0
What's a succinct way of ranging through N variables of any type to perform an operation?
Let's say I have variables a, b, c, d, e and want to go through all of them performing some operation.
asked 2 mins ago
1 Answer
Vote count:
0
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 Foo1 {
int foo() const { return 42; }
};
struct Foo2 {
int bar = 0;
int foo() { bar = 24; return bar; }
};
int main() {
using namespace std;
using namespace boost::fusion;
Foo1 foo1;
Foo2 foo2;
for_each(tie(foo1, foo2), [](auto &foo) {
cout << foo.foo() << endl;
});
cout << "foo2.bar after mutation: " << foo2.bar << endl;
}
answered 1 min ago
Aucun commentaire:
Enregistrer un commentaire