I want to define a INITIALIZE(...) that achieves the following
struct MyStruct {
std::string a;
bool b;
char c;
double d;
INITIALIZE(a, (b, true), (c, '$'), d);
};
gets expanded to
struct MyStruct {
std::string a;
bool b;
char c;
double d;
void init() {
a = get_value<std::string>(); // use return value of get_value function
b = true; // use init value from the macro
c = '$'; // use init value from the macro
d = get_value<double>(); // use return value of get_value function
}
};
Are these achievable with the help of Boost_PP macros?
T get_value<T>() is defined in a library.
This is almost doable, you need an extra pair of parens around things initialized by default.
Now
INITIALIZE((a), (b, 'a'), (c))expands toEDIT: As suggested by @Quentin, you can get rid of extra parentheses by using a macro similar to this. Necessary changes are
Then
INITIALIZE(a, (b, 'a'), c)also works.Edit: a better, less obstructive syntax could be possible, depending on what exact C++ construct we generate. See http://aantron.github.io/better-enums/ for inspiration.