为什么接受默认的 noexcept 移动构造函数?

Why is default noexcept move constructor being accepted?

假设以下 c++17 代码:

#include <type_traits>
namespace dtl
{
   struct One
   {
      explicit One(int);
      ~One() = default;
      One(const One &) = delete;
      auto operator=(const One &) -> One & = delete;
      auto operator=(One &&) -> One & = delete;
      One(One &&); // Throwable, not default;
      int m_int;
   };
   struct Two 
   {
      explicit Two(int);
      ~Two() = default;
      Two(const Two &) = delete;
      auto operator=(const Two &) -> Two & = delete;
      auto operator=(Two &&) noexcept -> Two & = delete;
      Two(Two &&) noexcept = default;
      One m_one;
   };
   One::One(One &&) { throw 1; }

   static_assert(std::is_nothrow_move_constructible_v<Two>);
}

Code at compiler-explorer

这里我们清楚地看到 class One 的移动构造函数没有标记为 noexcept。 Class Two 有一个默认的移动构造函数,明确要求是 noexcept。

如果我们检查这段代码,它会使用 GCC trunk、Clang trunk、MSVC 19.28 进行编译,而使用 MSVC19.24 会失败。

我检查了以下来源,它们似乎告诉我需要删除 Two 的移动构造函数:

CWG issue 1778 阅读 (N4296 [dcl.fct.def.default]/p3):

If a function that is explicitly defaulted is declared with an exception-specification that is not compatible (15.4) with the exception specification on the implicit declaration, then

if the function is explicitly defaulted on its first declaration, it is defined as deleted; otherwise, the program is ill-formed.

基于这些信息,我只能得出结论,所有 3 个编译器都错误地将 Two 视为 no_throw_move_constructible,应该隐式删除移动构造函数。由于所有 3 人都忽略此标准很奇怪,所以我想知道:这真的是编译器错误还是我遗漏了什么。

我相信您正在查看过时的信息。 DR1778 已被 P1286R2. If you look at the implementation status 取代,您将看到 gcc 10 和 clang 9 实现了这一新决议。

的确,如果你在 godbolt 中回到旧的 gcc 版本,它会告诉你:

<source>: In function 'int main()':
<source>:35:25: error: use of deleted function 'dtl::Two::Two(dtl::Two&&)'
   35 |     auto b = std::move(a);
      |                         ^
<source>:23:7: note: 'dtl::Two::Two(dtl::Two&&) noexcept' is implicitly deleted because its exception-specification does not match the implicit exception-specification ''
   23 |       Two(Two &&) noexcept = default;
      |       ^~~
Compiler returned: 1

您可以找到 gcc 讨论 here. According to this list,P1286R2 被接受为 DR,这意味着它被追溯应用于以前的标准。因此,较新的编译器将以您注意到的方式运行,而与所选的 C++ 标准无关。

然而,在运行时,这将按预期失败:

dtl::One::One(int) {};
dtl::Two::Two(int) : m_one(0) {};

int main() {
   auto a = dtl::Two{1};
   try {
      auto b = std::move(a);
   } catch (...) {
      // Even though an exception is thrown, it will not be caught here because
      // we broke our `noexcept` promise.
      std::cout << "caught" << std::endl;
   }
   return 0;
}
[:~/tmp] $ /usr/local/Cellar/llvm/11.0.0/bin/clang++ -std=c++17 mv.cpp  && ./a.out
libc++abi.dylib: terminating with uncaught exception of type int
Abort trap: 6