Insert comment characters using a preprocessor macro

I am trying to create a macro that allows me to model the behavior below, but this does not work. Can I insert comment characters with a macro? What else is needed?

#define model_interface(CLASS, ROOT) \ class CLASS : public NInterface<ROOT> { \ private: \ CLASS(CLASS&) { } \ // 'two slashes should be actually inserted too so another characters on same row are ignored' model_interface(Element, ElementRoot) { // 'previous bracket should be ignored' // members declarations here } 
+5
source share
1 answer

I don’t think that this is possible with the syntax you want, but it can be done with a little different - with parentheses.

This solution uses variable macros that are available with C + 11, but some compilers have supported it long before that.

 #define model_interface(CLASS, ROOT, ...) \ class CLASS : public NInterface<ROOT> { \ private: \ CLASS(CLASS&) { } \ __VA_ARGS__ \ } model_interface(Element, ElementRoot, // members declarations here // the variadic part takes care of a comma, eg: std::array<int, 3> a; ); 
+2
source

Source: https://habr.com/ru/post/1237685/


All Articles