我怎么能声明一个包装类型X 的移动构造函数,取决于is

编程入门 行业动态 更新时间:2024-10-27 10:33:13
我怎么能声明一个包装类型X 的移动构造函数,取决于is_nothrow_move_constructible ?(How can I declare a move constructor of a wrapper type X noexcept depending on is_nothrow_move_constructible?)

假设我有一个包装类型

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) {} };

更多推荐

本文发布于:2023-04-29 09:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   声明   类型   我怎么能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!