static_assert - a way to dynamically customize error message

后端 未结 3 631
粉色の甜心
粉色の甜心 2021-01-07 21:20

Is there a way to make static_assert\'s string being dynamically customized and then displayed?
What I mean is something like:

//pseudo code
static_ass         


        
3条回答
  •  时光说笑
    2021-01-07 22:18

    No, there is not.

    However this does not matter so much, because static_assert are evaluated at compile-time, and in case of error the compiler will not only print out the message itself, but it will also print the instanciation stack (in case of templates).

    Have a look at this synthetic example in ideone:

    #include 
    
    template 
    struct IsInteger { static bool const value = false; };
    
    template <>
    struct IsInteger { static bool const value = true; };
    
    template 
    void DoSomething(T t) {
      static_assert(IsInteger::value, // 11
      "not an integer");
    
      std::cout << t;
    }
    
    int main() {
      DoSomething("Hello, World!"); // 18
    }
    

    The compiler does not only emits the diagnostic, but it also emits the full stack:

    prog.cpp: In function 'void DoSomething(T) [with T = const char*]':
    prog.cpp:18:30:   instantiated from here
    prog.cpp:11:3: error: static assertion failed: "not an integer"
    

    If you know Python or Java and how they print the stack in case of exception, it should be familiar. In fact, though, it's even better, because you not only get the call stack, but you also get the arguments values (types here)!

    Therefore, dynamic messages are not as necessary :)

提交回复
热议问题