简体   繁体   English

为什么在C ++ 11/14中没有std :: move_if_noexcept对应std :: forward?

[英]Why there is no std::move_if_noexcept counterpart for std::forward in C++11/14?

I have watched Scott Meyers' talk on GoingNative2013 "An Effective C++11/14 Sampler" and he explained the use of std::move_if_noexcept . 我看过Scott Meyers关于GoingNative2013“一个有效的C ++ 11/14采样器”的演讲,他解释了使用std::move_if_noexcept

So in my opinion there should be a std::forward_if_noexcept to also guarantee exception-safety for forward ? 所以在我看来应该有一个std::forward_if_noexcept来保证forward异常安全吗? Why is there nothing like this in the Standard Library? 为什么标准库中没有这样的内容? Is there another possibility to guarantee it? 还有其他可能保证吗?

The forward semantics already are conditional, ie, they preserve the value category of its argument. 前向语义已经是有条件的,即它们保留了其参数的值类别。 The move, on the other hand, unconditionally changes (from an l-value to r-value) the value category of its argument to an r-value (reference), the std::move_if_noexcept obtains the resultant value category based on whether or not the move constructor throws no exceptions, effectively also making it conditional. 另一方面,此移动无条件地将其参数的值类别(从l值到r值)更改为r值(引用), std::move_if_noexcept基于是或std::move_if_noexcept获取结果值类别不是移动构造函数不会抛出任何异常,也有效地使它成为有条件的。

In short, std::forward doesn't change anything , it preserves the value category. 简而言之, std::forward不会改变任何东西 ,它会保留值类别。 std::move on the other hand, does change the value category. 另一方面, std::move确实改变了值类别。 So std::move_if_noexcept is introduced to say that if std::move 's change could cause an exception, then don't change anything ; 因此引入std::move_if_noexcept来说如果std::move的更改可能导致异常,那么不要改变任何东西 ; don't move it. 不要动它。

I think the rationale here also goes to the intended idiomatic use of all things std::move and std::forward respectively. 我认为这里的基本原理也分别是std::movestd::forward所有东西的惯用语。 In one of Scott's other talks (possibly at C++ and Beyond 2012) on "universal references", forwarding and moving, he was quite emphatic on the idiomatic use of forward, and I think this underpins a lot of what std::forward is and how it is used. 在斯科特关于“普遍引用”,转发和移动的其他一次谈话(可能在C ++和2012年之后)中,他非常强调前锋的习惯用法,我认为这支持了很多std::forward和它是如何使用的。 It may also just be as simple as it was never considered. 它也可能就像从未考虑过一样简单。

I would argue that there is no need, or at least there is diminished need when the std::forward is effected. 我认为没有必要,或者至少在std::forward实现时需求减少。

Given that there may be a use case for it, implementing one would not be too hard; 鉴于可能有一个用例,实施一个不会太难; but I'm not sure as to the general use case. 但我不确定一般用例。 It could be argued that the conditions related to the possible exceptions, and the required exception guarantees, should be tested for and dealt with outside of the function doing the forwarding . 可以认为,应该在执行转发的功能之外测试和处理与可能的异常相关的条件以及所需的异常保证。

template <class U, class T>
U wrapper(T&& arg)
{
    return U(forward<T>(arg));
}

template <class U, class T>
U method() 
{
  T t;
  // work with t
  return wrapper<U>(move_if_noexcept(t));
}

Granted, it is "early days" for a lot of this, so that may change. 当然,对于很多这种情况来说,它是“早期的”,因此可能会发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM