Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use macro define between module interface and implementation unit

I have a module interface unit in a SomeModule.ixx file, with some functions prototypes generated using a preprocessor macro:

export module SomeModule;

#define GENERATED_FUNCTION(ret, name, ...) typedef ret name##_prototype(__VA_ARGS__); \

#define GENERATED_FUNCTIONS_LIST \
GENERATED_FUNCTION(void, some_function, int param1, double param2) \
GENERATED_FUNCTION(void, some_function2, int param1, float param2) \
GENERATED_FUNCTION(void, some_function3, int* param1, int** param2) \

GENERATED_FUNCTIONS_LIST

#undef GENERATED_FUNCTION

export struct SomeStruct {
    void init();

#define GENERATED_FUNCTION(ret, name, ...) \
name##_prototype* name;

    GENERATED_FUNCTIONS_LIST
#undef GENERATED_FUNCTION
};

and an implementation unit in SomeModuleImpl.cpp :

module SomeModule;

void SomeStruct::init() {

#define GENERATED_FUNCTION(ret, name, ...) \
    name = NULL;

    GENERATED_FUNCTIONS_LIST
#undef GENERATED_FUNCTION

}

I want to set some value for the methods in SomeStruct, again using the macro, however I’m getting this error in the module implementation:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

E0020   identifier "GENERATED_FUNCTIONS_LIST" is undefined  

Is there any way to share macro defines between module interface and implementation?

>Solution :

Is there any way to share macro defines between module interface and implementation?

Yes, the same one you used to use: a header file. Put your GENERATED_FUNCTIONS_LIST macro in a header file. This is pretty standard for X-macro-style code.

Modules don’t include macros, so it doesn’t change much of what you have to do there.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading