For identical static_assert messages, should I rely on MACROS?

347 views Asked by At

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

There are 1 answers

4
Cheers and hth. - Alf On

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

#pragma once
// Copyright (c) 2013 Alf P. Steinbach

// The "..." arguments permit template instantiations with "<" and ">".

#define CPPX_STATIC_ASSERT__IMPL( message_literal, ... ) \
    static_assert( __VA_ARGS__, "CPPX_STATIC_ASSERT: " message_literal  )

#define CPPX_STATIC_ASSERT( ... ) \
    CPPX_STATIC_ASSERT__IMPL( #__VA_ARGS__, __VA_ARGS__ )

// For arguments like std::integral_constant
#define CPPX_STATIC_ASSERT_YES( ... ) \
    CPPX_STATIC_ASSERT__IMPL( #__VA_ARGS__, __VA_ARGS__::value )

As you can see there are some subtleties involved even when the compiler does have static_assert.