A technique for asserting that certain C++ constructs intentionally fail to compile, using namespace bait functions, decltype, static_assert, and std::is_same. By placing a catch-all overload in a bait namespace and using inline namespaces for namespaced functions, illegal calls resolve to a sentinel 'illegal' return type. A static_assert then verifies the return type matches 'illegal', causing a compile error if the call unexpectedly resolves to a valid overload. A helper macro ASSERT_COMPILATION_ERROR wraps the pattern for readable test code.

3m read timeFrom playfulprogramming.com
Post cover image
Table of contents
Building traps Copy link Link copied!

Sort: