伪造constexpr里面的static_assert的最佳方法?

您可能知道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)];
    }
}
zhanlongjie 回答:伪造constexpr里面的static_assert的最佳方法?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1438541.html

大家都在问