c++ - static_assert - a way to dynamically customize error message -
is there way make static_assert's string being dynamically customized , displayed?
mean like:
//pseudo code static_assert(check_range<t>::value, "value of " + typeof(t) + " type not ;)");
no, there not.
however not matter much, because static_assert
evaluated @ compile-time, , in case of error compiler not print out message itself, print instanciation stack (in case of templates).
have @ 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 integer"); std::cout << t; } int main() { dosomething("hello, world!"); // 18 }
the compiler not emits diagnostic, emits full stack:
prog.cpp: in function 'void dosomething(t) [with t = const char*]': prog.cpp:18:30: instantiated here prog.cpp:11:3: error: static assertion failed: "not integer"
if know python or java , how print stack in case of exception, should familiar. in fact, though, it's better, because not call stack, arguments values (types here)!
therefore, dynamic messages not necessary :)
Comments
Post a Comment