Is there a static_assert replacement which satisfies the C99 standard?

前端 未结 2 1891
刺人心
刺人心 2021-02-13 18:19

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 te

2条回答
  •  后悔当初
    2021-02-13 19:26

    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

提交回复
热议问题