2015-12-08 21 views
5

我正在嘗試創建一個允許我模擬下面的行爲的宏,但這不起作用。是否可以通過宏插入註釋字符?另一種選擇是什麼?使用預處理器宏插入註釋字符

#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 

} 
+0

有些編譯器(至少有一個?)支持'#define COMMENT/## /'發表評論,但我不知道這是不是你要找的。無論如何它都是非標準的,所以它不是首選解決方案。 –

+0

當*調用*時,大多數宏觀內容不會被人類看到,因此註釋並不真正有用。通常,編譯器使用預處理器*生成新代碼*,然後代碼由編譯器處理。一些編譯器可以選擇在預處理階段後打印源代碼。 –

回答

2

我不認爲它可能與你想要的語法,但它是可行的一個稍微不同的一個 - 使用括號。

該解決方案使用可變參數宏,自C + 11以後可用,但有些編譯器在此之前支持它。

#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; 
);