您可能知道static_assert并不关心它是否在constexpr的“活动”分支内(始终“运行”)。
我有一个hack解决方法,该方法使用无效的数组维来触发错误,但这很丑陋。 C ++ 20中有更好的解决方案吗?
// does not work
template<typename T>
void argh(){
if constexpr(sizeof (T)<123456 ) {
} else {
static_assert(0);
}
}
// works but confusing to novice programmers,both code and error
template<typename T>
void meh(){
if constexpr(sizeof (T)<123456 ) {
} else {
int error_triggering[-sizeof(T)];
}
}