static_assert - a way to dynamically customize error message

吃可爱长大的小学妹 提交于 2019-12-19 05:18:49

问题


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_assert(Check_Range<T>::value, "Value of " + typeof(T) + " type is not so good ;)");

回答1:


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

template <typename T>
struct IsInteger { static bool const value = false; };

template <>
struct IsInteger<int> { static bool const value = true; };

template <typename T>
void DoSomething(T t) {
  static_assert(IsInteger<T>::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 :)




回答2:


The standard specifies the second argument of static_assert to be a string literal, so no chance for computation there as far as I can see (except for preprocessor macros).

A compiler could extend the standard and allow const-expressions of approporiate type in this position, but I have no idea if any compiler does.




回答3:


As Matthieu said, it's not possible, but you can get some of the functionalities you're looking for by using macros:

#define CHECK_TYPE_RANGE(type)\
    static_assert(Check_Range<type>::value, "Value of " #type " type is not so good ;)");

CHECK_TYPE_RANGE(float); // outputs "Value of float type is not so good ;)"


来源:https://stackoverflow.com/questions/7778734/static-assert-a-way-to-dynamically-customize-error-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!