I'm watching CppCon 2015: Scott Schurr “constexpr: Applications", on 19'28" he's showing a trick to force compile time error only:
A Way to Force Compile-Time Only?
- Not within the standard
- But a hack that sometimes works
Unresolved Symbol In Throw
extern const char* compile11_bin_invoked_at_runtime; template <typename T = std::uint32_t> constexpr T compile11_bin( constexpr_txt t, std::size_t i = 0, // index std::size_t b = 0, // bit count T x = 0) // accumulator { return i >= t.size() ? x : // end recursion b >= std::numeric_limits<T>::digits ? throw std::overflow_error("Too many bits!") : t[i] == ',' ? compile11_bin<T>(t, i+1, b, x) : t[i] == '0' ? compile11_bin<T>(t, i+1, b+1, (x*2)+0) : t[i] == '1' ? compile11_bin<T>(t, i+1, b+1, (x*2)+1) : throw std::domain_error( // Only '0', '1', and ',' compile11_bin_invoked_at_runtime); }
I'm curious here, what's the motivation to force a constexpr to have compile time error only?