Reputation: 3111
I would like to define a template like this:
#define DECLARE_MY_STRUCT(name) \
#ifndef MY_STRUCT_DECLARED \
struct my_##name##_struct { \
double var; \
}; \
#define MY_STRUCT_DECLARED \
#endif
This would allow me to use DECLARE_MY_STRUCT template wherever I want and not get "my_struct was already defined" error.
Unfortunately, gcc treats #ifndef as its preprocessor directives instead of part of template declaration and fails to build such code. Any way to workaround this? ( Except for using
#ifndef MY_X_STRUCT_DECLARED
DECLARE_MY_STRUCT(X)
#endif
as there may be a lot of different struct names. )
Upvotes: 1
Views: 50
Reputation: 72639
The C Standard does not allow nested pre-processor directives, so this is impossible. The #
in #ifndef
would be treated as a stringizing #
.
Upvotes: 2