如果成员具有非平凡的noexcept赋值运算符,则默认的移动赋值不能明确地为noexcept

时间:2013-12-16 12:10:29

标签: c++ c++11 move-semantics assignment-operator

此代码无法使用gcc 4.8.2(-std = c ++ 11)编译,但使用clang 3.4(trunk)编译(-std = c ++ 11):

#include <type_traits>
#include <vector>

struct X {
  X& operator=(X&&) noexcept = default;
  // adding noexcept this leads to an error in gcc, but works in clang:
  // function ‘X& X::operator=(X&&)’ defaulted on its first
  // declaration with an exception-specification that differs from the
  // implicit declaration ‘X& X::operator=(X&&)’

  std::vector<char> m;
};

// this assert holds, even without noexcept
static_assert(std::is_nothrow_move_assignable<X>::value, 
              "type-specification violation");

int main()
{
  return 0;
}

static_assert是gcc中有趣的部分:默认的移动分配将是noexcept,但我不能这样声明。

不涉及vector的变体是:

template<bool b>
struct F {
  F& operator=(F&&) noexcept(b) {return *this;}
};

struct X {
  X& operator=(X&&) noexcept = default;
  F<true> f;
};

这里的预期行为是什么?直觉铿锵似乎是正确的。

0 个答案:

没有答案