Is there way to check the type of a preprocessor symbol value in C/C++ -
Is there way to check the type of a preprocessor symbol value in C/C++ -
parts of code depend on value of preprocessor symbol:
int a() { #if sdk_version >= 3 homecoming 1; #else homecoming 2; #endif }
the comparing depends of value of sdk_version. expected integer or compares integer, in case, 3. if sdk_version cannot compared integer, there compile error.
is there way abort compilation if sdk_version not of expected type? example:
#if type(sdk_version) != int # not compile, know #error "sdk_version must integer." #endif
use template generate such error:
template<typename t> struct macro_error; template<> struct macro_error<int> {}; template<typename t> void check(t) { macro_error<t> sdk_version_must_be_int; } int ignored = (check(sdk_version), 0);
this code generate compilation error, having next string in it, if sdk_version
not int:
sdk_version_must_be_int
see these demo:
http://ideone.com/mwxim (error :#define sdk_version 1.0
) http://ideone.com/crjxn (ok : #define sdk_version 1
) and notice error message in first case. prints this:
prog.cpp:9: error: ‘sdk_version_must_be_int’ has incomplete type prog.cpp:9: warning: unused variable ‘sdk_version_must_be_int’
c++ c types preprocessor
Comments
Post a Comment