static_assert具有以下语法,它表明需要字符串文字。
static_assert ( bool_constexpr ,字符串字面量);
由于在编译时无法观察到字符串的实例,因此以下代码无效:
const std::string ERROR_MESSAGE{"I assert that you CAN NOT do this."};
static_assert(/* boolean expression */ ,ERROR_MESSAGE);
我的代码中到处都是静态断言,它们说的是相同的错误消息。由于需要字符串文字,最好用 MACRO 替换所有重复的字符串文字,还是有更好的方法?
// 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_);