M
Marcus Kwok
I am designing a GUI (my question is not about GUIs) and I have named my
variables using a regular naming scheme. However, in order to simplify
the code using these variables, I have created an array of non-owning
pointers to these variables. I am trying to write a macro to generate
these variable names for me, but I am not sure if what I want to do is
possible.
The code below demonstrates what I want to do (except it is generating
function names instead of variable names). I would like to have a macro
that I can put in a loop to generate the various names. However, since
macros are expanded before the compilation phase, I know this won't work
like this (in the loop, the macro gets expanded to method_i_do() since
the actual values for 'i' do not get set until runtime).
If it is possible at all, I am seeking a simple solution. There are 45
variables, which can be grouped into sets of 5 that I will apply the
solution to. It's not a matter of laziness (I have already typed the
variable names explicitly); I would just like to avoid having lots of
lines of trivial code that are all very similar, but only if the
overhead of implementing the solution is not significantly greater than
listing all 45 variables.
Is this possible using templates, or any other standard technique?
#include <iostream>
void method_1_do() { std::cout << "Method 1\n"; }
void method_2_do() { std::cout << "Method 2\n"; }
void method_3_do() { std::cout << "Method 3\n"; }
#define METHOD_DO(pre, i, post) pre ## i ## post()
int main()
{
METHOD_DO(method_, 1, _do);
METHOD_DO(method_, 2, _do);
METHOD_DO(method_, 3, _do);
/*
for (int i = 0; i < 3; ++i) {
METHOD_DO(method_, i, _do);
}
*/
return 0;
}
variables using a regular naming scheme. However, in order to simplify
the code using these variables, I have created an array of non-owning
pointers to these variables. I am trying to write a macro to generate
these variable names for me, but I am not sure if what I want to do is
possible.
The code below demonstrates what I want to do (except it is generating
function names instead of variable names). I would like to have a macro
that I can put in a loop to generate the various names. However, since
macros are expanded before the compilation phase, I know this won't work
like this (in the loop, the macro gets expanded to method_i_do() since
the actual values for 'i' do not get set until runtime).
If it is possible at all, I am seeking a simple solution. There are 45
variables, which can be grouped into sets of 5 that I will apply the
solution to. It's not a matter of laziness (I have already typed the
variable names explicitly); I would just like to avoid having lots of
lines of trivial code that are all very similar, but only if the
overhead of implementing the solution is not significantly greater than
listing all 45 variables.
Is this possible using templates, or any other standard technique?
#include <iostream>
void method_1_do() { std::cout << "Method 1\n"; }
void method_2_do() { std::cout << "Method 2\n"; }
void method_3_do() { std::cout << "Method 3\n"; }
#define METHOD_DO(pre, i, post) pre ## i ## post()
int main()
{
METHOD_DO(method_, 1, _do);
METHOD_DO(method_, 2, _do);
METHOD_DO(method_, 3, _do);
/*
for (int i = 0; i < 3; ++i) {
METHOD_DO(method_, i, _do);
}
*/
return 0;
}