I have been trying to implement a method similar to static_assert
which is defined in the C++11 standard. The main problem is how does the C++ compiler write the text message being passed to static_assert
as a const char*
? I can get the compiler to write a message like A_is_not_POD
. This is what I have:
#define MY_STATIC_ASSERT(condition, name) \
typedef char name[(condition) ? 1 : -1];
But it would be quite nice to get the compiler to write something like "Error: A is not POD."
Any suggestions?
In the c99 standard there is no "official" way to perform a static assertion in your C++ compiler.
The C++ compiler detects an error in the code and prints out an appropriate error message based on a standard list of messages it has for each error that is known it can encounter. In c99, the compiler doesn't know what a "static assert error" is, so you need to cause some other error.
However, because creating static asserts with a c99 compiler is kind of a hack, it is not capable of printing out a nice error message exactly how you want.
For your example,
will trigger the
"error: size of array ‘my_error_msg’ is negative"
message in the gcc compiler (and should a similar message in other compilers, you hope!). Giving the array an error message for thename
was a way to print out your own info. There are various other techniques/hacks you can do on purpose such as bad templates, enums linkNote: you can provide custom compiler messages prior to C++11 using pre-processor macros such as
#error
or#pragma
. However pre-process time is not the same as compile-time! The the pre-processor has a limited ability to evaluate many expressions and keywords such as"if"
,"sizeof"
,"return"
and so on have no meaning to the pre-processor, only the compiler. link with some overview