假设我有一个包装类型
template <typename T> struct X {/*..*/};我不能只是X(X&&) = default因为我必须在那里做非平凡的事情。
但是,我希望它是noexcept但只有在T(T&&)是noexcept 。 这可以用::std::is_nothrow_move_constructible进行测试。
我不知道如何有条件地启用构造函数的一个版本或取决于constexpr的其他版本。 我想可能有一种使用SFINAE的方法,但我不知道如何将它应用到ctors。
Assume I have a wrapper type
template <typename T> struct X {/*..*/};and I cannot just X(X&&) = default because I have to do non-trivial stuff there.
However, I want it to be noexcept but only in case that T(T&&) is noexcept. This can be tested with ::std::is_nothrow_move_constructible.
I'm at a loss how to conditionally enable one version of the constructor or the other depending on a constexpr. I suppose there could be a way to use SFINAE, but I don't see how to apply it to ctors.
最满意答案
noexcept说明符接受任何布尔常量表达式,所以你可以直接在你的类型特征中检查:
template <typename T> struct X { X(X&&) noexcept(std::is_nothrow_move_constructible<T>::value) {} };The noexcept specifier accepts any boolean constant expression, so you can but your type trait check in there directly:
template <typename T> struct X { X(X&&) noexcept(std::is_nothrow_move_constructible<T>::value) {} };更多推荐
发布评论