12

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?

6
  • 1
    No that's not possible. The only thing you can get with older standards, is to spit out variable names. Commented Dec 6, 2013 at 14:12
  • 1
    “I can get the compiler to write a message like A_is_not_POD” How about showing what you have already done to achieve that? It would make your question clearer. The FAQ used to say “the best questions have a bit of source code in them” before it was re-revamped. Commented Dec 6, 2013 at 14:14
  • That's a shame. But where can we find the source code of static_assert function? The interesting thing is how does the compiler write the string to the compiler message. I have been trying to use templates somehow but have been unsuccessful so far. Commented Dec 6, 2013 at 14:14
  • 4
    @Shubham C++11's static_assert is not a function - it's a keyword, just like for. There's no source code for for either. That's why the compiler can do anything it really wants with the arguments. Commented Dec 6, 2013 at 14:17
  • I would guess it's impossible, since Boost's static assert can't print messages in C++03. Presumably, they tried pretty hard to find a way; and if it's impossible in C++03, it's almost certainly impossible in C99. Commented Dec 6, 2013 at 14:51

2 Answers 2

6

Not sure i understand question, but C11 have _Static_assert(condition, errmessage). In C99 this functionality was missing but, depending on compiler, it could be possible to emulate. E.g. for gcc (unfortulately clang doesn't support attribute(error))

#define MY_STATIC_ASSERT(cnd, descr) ({ \ extern int __attribute__ ((error("static assert failed: (" #cnd ") (" #descr ")"))) \ compile_time_check(void); \ ((cnd) ? 0 : compile_time_check()), 0; \ }) 
Sign up to request clarification or add additional context in comments.

1 Comment

That's really nice. I didn't know we could do something like this!!! Its a shame clang doesn't provide this feature. Will however, will have a look around. Thanks a lot!!!
3

In the c99 standard there is no "official" way to perform a static assertion in your C++ compiler.

"The main problem is how does the C++ compiler write the text message being passed to static_assert as a const char*?"

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,

#define MY_STATIC_ASSERT(condition, name) \ typedef char name[(condition) ? 1 : -1]; MY_STATIC_ASSERT(false, my_error_msg) 

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 the name 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 link

Note: 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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.