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_);