Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert comment characters using preprocessor macro

Tags:

c++

I am trying to create macro that allows me to simulate behaviour below, but this doesn't work. Is it possible to insert comment characters by macro? What is another option?

#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

}
like image 260
Krab Avatar asked Oct 30 '22 13:10

Krab


1 Answers

I don't think it's possible with the syntax you want, but it's doable with a slightly different one - using parentheses.

This solution uses variadic macros, which is available since C+11, but some compilers supported it well 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, e.g.: std::array<int, 3> a;
);
like image 130
Karoly Horvath Avatar answered Nov 15 '22 05:11

Karoly Horvath