Consider the following struct:
struct Test {
char a;
short b;
int c;
long long d;
void transformTest() {
// Pseudo
foreach datamember (regardless of type) of Test
call someTransform(datamember)
}
};
We could also pass a lambda, function pointer, functor, whatever into the transformTest(), that's not my concern as of right now.
What's the best way to do this?
The best way is to do it explicitly:
someTransform(a);
someTransform(b);
someTransform(c);
someTransform(d);
Of course, you'll need an appropriate number of overloads of someTransform()
.
If you really, really don't like that, there's always Boost Fusion. With that you can put your values together in a structure which the library understands and then can iterate over. This won't be worth doing for simple use cases.
Sounds like a case for Boost Fusion and its for_each()
function combined with BOOST_FUSION_ADAPT_STRUCT. This stuff can work some miracles! Here's an example on how you can do it:
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <iostream>
using namespace boost::fusion;
struct Print
{
template <typename T>
void operator()( T && t ) const
{
std::cout << t << ' ';
}
};
struct Test {
char a;
short b;
int c;
long long d;
void printTest() const;
};
BOOST_FUSION_ADAPT_STRUCT(
Test,
(char, a)
(short, b)
(int, c)
(long long, d)
)
void Test::printTest() const
{
for_each ( *this, Print() );
}
int main()
{
const auto t = Test();
t.printTest();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With