Trigger a compile error when a variable name is used
I am using a tool called Rational Rose for C++. When a variable name which includes the word "interface" is defined, the tool stops working and prints out a blank error message.
The support issue can be seen here: https://www-304.ibm.com/support/docview.wss?uid=swg21271841&wv=1
I would like to add a preprocessor di开发者_JAVA技巧rective such that when the variable name "interface" is used, a compile error will be displayed.
Something along the lines of:
#define interface #error The Keyword interface is not permitted
You can't use preprocessor statements inside preprocessor statements.
However, you can force a compiler error along these lines:
#define interface -ERROR_interface_is_a_reserved_symbol
On windows (MS compiler) this would point you to the code line with the error:
yourfile.cpp(82): error C2065: 'ERROR_interface_is_a_reserved_symbol' : undeclared identifier
Would agree with others here though that it seems to be the wrong place for a solution to your problem.
EDIT: As pointed out by DeadMG, this solution would only work if you're looking for exact variable names, rather than partial matches.
You can't use the preprocessor to gain partial matches. If I write IInterface
and EInterface
and ILikeBigInterfacesAndICannotLie
, then you cannot use the preprocessor to match them all.
精彩评论