i have following code working under msvc 2015:
#define class_js_psg_property_ex(property, value) \ static bool get##property(/*irrelevant params here...*/) \ { \ ... particular code ... return true; \ } #define class_js_psg_property(value) \ class_js_psg_property_ex(##value, value) ... #define kprop 1 class_js_psg_property_ex(version, kprop) class_js_psg_property(kprop)
this should define methods named getversion
, getkprop
.
now, gives following error under gcc c++14 (actually tdm-gcc-64):
pasting "(" , "kprop" not give valid preprocessing token
how should written in order compile under gcc c++14 , msvc 2015?
the trick - if don't want name expanded macro, must pass ##
operator right away - result of concatenation must valid token. this:
#include <iostream> #define class_js_psg_property_ex_helper(getname) \ static bool getname() { return true; } #define class_js_psg_property_ex(property, value) \ class_js_psg_property_ex_helper(get##property) #define class_js_psg_property(value) \ class_js_psg_property_ex_helper(get##value) #define kprop 1 class_js_psg_property_ex(version, kprop) class_js_psg_property(kprop) int main() { std::cout << getversion() + getkprop(); }
the reason original code appears work msvc because msvc preprocessor famously non-conforming - operates on stream of characters (wrong), rather stream of tokens (right). in class_js_psg_property_ex(##value, value)
, ##
not unary operator suggest - it's binary operator glues (
, value
single token (value
. not valid preprocessing token, program ill-formed, gcc complains about. msvc preprocessor later breaks nonsensical token pieces (which conforming preprocessor never do).
Comments
Post a Comment