I work on a very large C++11 codebase, and we want to prevent future developers from declaring variables as long, since using long causes some issues when compiling on Windows compared to Linux.
Our project uses several external libraries that include declarations with long, but these should not be used directly in our code. Is there any way (without modifying the .h and .cpp files of our project) to prevent developers from declaring variables as long in the future?
I have tried (and failed) to use -include forbid_long.h, where forbid_long.h contains a macro like #define long static_assert(...), but this breaks the build due to conflicts with standard headers.
Is there any way to cause a compile-time error if someone declares a long?
I have no idea about the efficacy of using it, but you can use poisoned identifiers:
#pragma GCC poison long
int main() {
long x;
return 0;
}
GCC:
$ g++ foo.cpp
test.cpp:4:3: error: attempt to use poisoned "long"
4 | long x;
| ^
Clang:
$ clang++ test.cpp
test.cpp:4:3: error: attempt to use a poisoned identifier
4 | long x;
| ^
The poison #pragma should appear after any #includes or else your system headers may not compile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With