static_assert has the following syntax, which states that a string literal is required.
static_assert ( bool_constexpr , string literal );
Since an instance of a string CAN'T be observed at compile time, the following code is invalid:
const std::string ERROR_MESSAGE{"I assert that you CAN NOT do this."};
static_assert(/* boolean expression */ ,ERROR_MESSAGE);
I have static asserts all over my code, which say the same error message. Since a string literal is required, would it be best to replace all the repetitive string literals with a MACRO, or is there a better way?
// Is this method ok?
// Should I hand type them all instead?
// Is there a better way?
#define _ERROR_MESSAGE_ "danger"
static_assert(/* boolean expression 1*/ ,_ERROR_MESSAGE_);
//... code ...
static_assert(/* boolean expression 2*/ ,_ERROR_MESSAGE_);
//... code ...
static_assert(/* boolean expression 3*/ ,_ERROR_MESSAGE_);
In C++ you should not define a constant as a macro. Define it as a constant. That's what constants are for.
Also, names beginning with underscore followed by uppercase letter, such as your
_ERROR_MESSAGE_
, are reserved to the implementation.That said, yes, it's good idea to use a macro for static asserts, both to ensure a correct string argument and to support compilers that possibly don't have
static_assert
, but this macro is not C style constant: it takes the expression as argument, and provides that expression as the string message.Here is my current
<static_assert.h>
:As you can see there are some subtleties involved even when the compiler does have
static_assert
.