Using a C-string const script as a compiler error message

Since the question suggests what I would like to do is

template<const char* Err>
struct broken
{
    template<typename... Args>
    constexpr broken(Args&&...)
    {
        //the sizeof... confuses the compiler as to only emit errors when instantiated
        //this does not work, static_assert only accepts string literals
        static_assert(sizeof...(Args) < 0, Err);
    }
};

I hope that it brokengenerates a compiler error message Errwhen instantiating. However, it static_assertonly accepts a string literal as the second argument. Is there a way to emit a string based compiler error constexpr?

+4
source share
1 answer

What you want cannot work in any form or form, because you can legally do

extern const char foo[];
template <const char* err> class broken {};
broken<foo> tisbroken;

and foodo not have to be defined in the current TU (or anywhere else in this case) for compilation.

ODR err foo , foo - undefined, .

, , , .

+1

Source: https://habr.com/ru/post/1675648/


All Articles