std::move_if_noexcept 调用复制赋值,即使移动赋值是 noexcept;为什么?

std::move_if_noexcept calls copy-assignment even though move-assignment is noexcept; why?

我正在尝试尽可能接近 Strong Exception Gua运行tee,但是在玩 std::move_if_noexcept 时我 运行 变成了一些看似奇怪的行为。

尽管下面class中的移动赋值运算符被标记为noexcept复制赋值运算符 在使用相关函数的 return 值调用时被调用。

struct A {
  A ()         { /* ... */ }
  A (A const&) { /* ... */ }

  A& operator= (A const&) noexcept { log ("copy-assign"); return *this; }
  A& operator= (A&&)      noexcept { log ("move-assign"); return *this; }

  static void log (char const * msg) {
    std::cerr << msg << "\n";
  }
};
int main () {
  A x, y;

  x = std::move_if_noexcept (y); // prints "copy-assign"
}

问题

简介

move_if_noexcept 的名称当然意味着函数将产生一个 rvalue-reference 只要此操作是 noexcept,并且在请注意,我们很快就会意识到两件事:

  1. T&T&&T const& 的简单转换永远不会抛出异常,那么这个函数的目的是什么?
  2. move_if_noexcept 如何神奇地推断出使用 returned 值的上下文?

实现的答案(2)既可怕又自然; move_if_noexcept 根本无法推断出这样的上下文(因为它不是思想-reader),这反过来意味着该功能必须按照一些静态规则集来发挥作用。


TL;DR

move_if_noexcept 将有条件地 return 一个 rvalue-reference 取决于参数的异常规范type 的 move-constructor,它仅用于初始化对象(即不分配给它们)。

template<class T>
void intended_usage () {
  T first;
  T second (std::move_if_noexcept (first));
}

更好的名字可以是 move_if_move_ctor_is_noexcept_or_the_only_option;虽然打字有点繁琐,但至少表达了预期的用途。


move_if_noexcept

的诞生

阅读产生 std::move_if_noexcept 的提案 (n3050),我们发现以下段落(强调我的):

We propose that instead of using std::move(x) in those cases, thus granting permission for the compiler to use any available move constructor, maintainers of these particular operations should use std::move_if_noexcept(x), which grants permission move unless it could throw and the type is copyable.

Unless x is a move-only type, or is known to have a nonthrowing move constructor, the operation would fall back to copying x, just as though x had never acquired a move constructor at all.


那么,move_if_noexcept 的作用是什么?

std::move_if_noexcept 将有条件地将传递的 lvalue-reference 转换为 rvalue-reference,除非;

  • 潜在的移动构造函数可能会抛出,并且;
  • 类型是CopyConstructible
// Standard Draft n4140 : [utility]p2

template<class T>
constexpr conditional_t<
  !is_nothrow_move_constructible::value && is_copy_constructible<T>::value,
  const T&, T&&
> move_if_noexcept (T& x) noexcept;

这基本上意味着它只会产生一个 rvalue-reference 如果它可以证明它是唯一可行的选择,或者如果它保证不会抛出异常(通过noexcept).

表示

判决

std::move 是对 rvalue-reference 的无条件转换,而 std::move_if_noexcept 取决于对象可以 移动的方式-constructed - 因此它应该只在我们实际构造对象的地方使用,而不是在我们分配给它们时使用。

您代码段中的 复制赋值运算符 被调用,因为 move_if_noexcept 找不到标记为 移动构造函数 noexcept,但是因为它有一个 copy-constructor 函数将产生一个类型,A const&,适合这样的。


请注意,复制构造函数符合MoveConstructible类型,这意味着我们可以move_if_noexcept return rvalue-reference 通过对您的代码段进行以下调整:

struct A {
  A ()                  { /* ... */ }
  A (A const&) noexcept { /* ... */ }
  
  ...
};

例子

struct A {
  A ();
  A (A const&);
};

A a1;
A a2 (std::move_if_noexcept (a1)); // `A const&` => copy-constructor
struct B {
  B ();
  B (B const&);
  B (B&&) noexcept;
};

B b1;
B b2 (std::move_if_noexcept (b1)); // `B&&` => move-constructor
                                   //          ^ it's `noexcept`
struct C {
  C ();
  C (C&&);
};

C c1;
C c2 (std::move_if_noexcept (c1)); // `C&&` => move-constructor
                                   //          ^ the only viable alternative
struct D {
  C ();
  C (C const&) noexcept;
};

C c1;
C c2 (std::move_if_noexcept (c1)); // C&& => copy-constructor
                                   //        ^ can be invoked with `T&&`

进一步阅读: