I am going through the first chapters of the book "Modern C++ Design". In particular, compile time assertions. I have some problem with the following code:
template<bool> struct CompileTimeChecker { CompileTimeChecker(...) {} }; template<> struct CompileTimeChecker<false> {}; #define STATIC_CHECK(expr, msg)\ {\ struct ERROR_##msg {ERROR_##msg() {}};\ CompileTimeChecker<((expr) != 0)>(ERROR_##msg());\ } int main() { STATIC_CHECK(0, MessageNull); STATIC_CHECK(1, MessageOne); } This doesn't raise a compile time error for g++ 7.4.0 and clang++ 6.0.0. However, the following code does raise an error (as expected):
template<bool> struct CompileTimeChecker { CompileTimeChecker(...) {} }; template<> struct CompileTimeChecker<false> {}; #define STATIC_CHECK(expr, msg)\ {\ struct ERROR_##msg {ERROR_##msg(int i) {i;}};\ CompileTimeChecker<((expr) != 0)>(ERROR_##msg(0));\ } int main() { STATIC_CHECK(0, MessageNull); STATIC_CHECK(1, MessageOne); } The only difference in the second code is the usage of a constructor with parameters.
The expected error message in both cases is:
- g++:
no matching function for call to ‘CompileTimeChecker<false>::CompileTimeChecker(main()::ERROR_MessageNull) - clang++:
no matching conversion for functional-style cast from 'ERROR_MessageNull' to 'CompileTimeChecker<(0 != 0)>'
static_assert?CompileTimeChecker<(expr)>, notCompileTimeChecker<(expr != 0)>, because this is a macro.static_assert.