'C++ interlace marco or template
Is there a marco or template can implement like
enum class Field {
MY_MACRO_ADD(name,std::string)
MY_MACRO_ADD(age,int)
will generate
enum class Field {
name, age
};
std::string get_name();
int get_age();
Solution 1:[1]
Sure. I make a simple version.
Put these codes in a single file. That's how you want to expand your macros look like. (I have no idea if you're going to customize your class name, so I put it also in the macro)
// macro.h
enum class STRUCT_NAME
{
#undef ADD_PROPERTY
#define ADD_PROPERTY(type, name) name,
STRUCT_BODY
};
#undef ADD_PROPERTY
#define ADD_PROPERTY(type, name) type get_##name();
STRUCT_BODY
Here is where you define your enum class.
// file where you define your enum
#define STRUCT_NAME Field
#define STRUCT_BODY \
ADD_PROPERTY(string, name) \
ADD_PROPERTY(int, age) \
Here is a demo. Hope my answer can help you.
You could try to improve my answer, or a repo can solve your problems in other ways.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nimrod |
