C++中是否可以創建變量,在每次使用不同的值後它會被展開?C++ define changeable宏
例如,我想,繼
#define mytype [smth here]
void foo(mytype a,mytype b,mytype c)
將擴大到
void foo(mytype1 a,mytype2 b,mytype3 c)
或
void foo(mytype1 a,mytype11 b,mytype111 c)
C++中是否可以創建變量,在每次使用不同的值後它會被展開?C++ define changeable宏
例如,我想,繼
#define mytype [smth here]
void foo(mytype a,mytype b,mytype c)
將擴大到
void foo(mytype1 a,mytype2 b,mytype3 c)
或
void foo(mytype1 a,mytype11 b,mytype111 c)
如果輸出時它應該只會增加,你可以定義一個結構重寫operator<<
,如:
struct myvarstruct {
int i;
myvarstruct() : i(1){}
} myvar;
ostream &operator << (ostream &o, myvarstruct &s) {
return o << "myvar" << s.i++;
}
我需要它在預處理/編譯期間增加,而不是運行時間。 – Somnium
以下是使用的示例3210實現的功能參數的事情:
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
void foo(BOOST_PP_ENUM_BINARY_PARAMS(3, mytype, p));
,將擴大到
void foo(mytype0 p0, mytype1 p1, mytype2 p2);
#define mytype [smth here]
#define TYPE(a, b) _TYPE(a, b)
#define _TYPE(a, b) a##b
void foo(TYPE(mytype, 1) a, TYPE(mytype, 2) b, TYPE(mytype, 3) c)
簡短的回答沒有;很長的答案:Nooooooooo – Raxvan
宏在預處理過程中展開。你不能。 – devnull
你爲什麼需要這個? –