我如何静态断言static_cast是noexcept?

我有一个仿函数,可以执行从任何类型到特定类型的静态转换,以这种方式定义:

template <typename T_Out>
struct cast_to {
    template <typename T_In>
    T_Out operator()(T_In&& value) const noexcept {
        return static_cast<T_Out>(value);
    }
};

Now, I would like to restrict the use of the functor to those static casts expressions that are declared, implicitly or explicitly, as noexcept. The idea is to add some static assertion in the operator() function. So far, I've tried with two constexpr expressions:

std::is_nothrow_constructible<T_Out, decltype(value)>::value

noexcept(static_cast<T_Out>(value)

两者似乎都按我的预期工作(实际上,我已经看到,至少在GCC上,第一个检查也包括另一个检查)。我应该选择哪种方法?有更好的选择吗?

评论