C++ 默认noexcept移动语义
我无法理解以下错误背后的原因 示例代码:C++ 默认noexcept移动语义,c++,c++11,gcc,move-semantics,noexcept,C++,C++11,Gcc,Move Semantics,Noexcept,我无法理解以下错误背后的原因 示例代码: #include <string> class Message { private: std::string from; std::string to; std::string message; public: Message() = default; Message(Message&&) noexcept = default; Message(const Message&) = defau
#include <string>
class Message {
private:
std::string from;
std::string to;
std::string message;
public:
Message() = default;
Message(Message&&) noexcept = default;
Message(const Message&) = default;
Message& operator=(Message&&) noexcept = default;
Message& operator=(const Message&) = default;
Message(const std::string& from,
const std::string& to,
const std::string& message)
: from(from),
to(to),
message(message)
{ }
std::string getFrom() const { return from; }
std::string getTo() const { return to; }
std::string getMessage() const { return message; }
};
int main() {
Message m("me", "you", "hello");
return 0;
}
编译器版本:
$ g++ --version
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
只需从默认的move赋值操作符中删除noexcept,就可以编译示例。但是我不明白为什么默认的移动构造函数允许noexcept,而默认的移动赋值操作符却不允许
有人能解释一下吗?g++4.8仍然在实现C++11。在服务器上运行代码不会产生错误。我要说的是,这是一个已经修复的bug,或者是一个尚未完成的支持。这是在4.9Verified中修复的。我构建了g++5.4,它没有问题。但值得注意的是,Clang3.8也有同样的问题。也许它与C++11标准有关?以下是用于参考的clang错误输出:$clang++-3.8-std=C++11-Wall noexcept.cpp noexcept.cpp:13:12:错误:显式默认移动分配运算符的异常规范与计算出的一条消息和运算符不匹配(&Message&)noexcept=默认值;^生成1个错误。
$ g++ --version
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.