此代码无法使用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;
};
这里的预期行为是什么?直觉铿锵似乎是正确的。