下面的代码可以使用Visual Studio 2015成功编译,但使用Visual Studio 2017失败.Visual Studio 2017报告:
错误C2280:"std :: pair :: pair(const std :: pair&)":尝试引用已删除的函数
#include <unordered_map>
#include <memory>
struct Node
{
std::unordered_map<int, std::unique_ptr<int>> map_;
// Uncommenting the following two lines will pass Visual Studio 2017 compilation
//Node(Node&& o) = default;
//Node() = default;
};
int main()
{
std::vector<Node> vec;
Node node;
vec.push_back(std::move(node));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
看起来Visual Studio 2017显式需要移动构造函数声明.是什么原因?
我正在将我的项目从VS2015迁移到VS2017,这当然不会顺利进行。
我看到奇怪的编译器错误,可以通过以下代码重现该错误:
struct MoveOnly
{
MoveOnly() {}
MoveOnly(const MoveOnly&) = delete;
MoveOnly& operator = (const MoveOnly&) = delete;
MoveOnly(MoveOnly&&) = default;
MoveOnly& operator = (MoveOnly&&) = default;
bool operator == (const MoveOnly& rhs)const{return false;}
};
struct Hasher
{
size_t operator()(const MoveOnly&)const{return 0;}
};
std::vector < std::unordered_map<MoveOnly, int, Hasher> > test;
test.emplace_back();
Run Code Online (Sandbox Code Playgroud)
我可以使用所有编译器(gcc 7.2,clang 5.0.0,icc 18以及MSVC 2015)成功编译此代码。请点击以下链接查看测试:https :
//godbolt.org/g/uSqwDJ。在MSVC 2017(19.10.25017)上,但是由于编译器尝试引用MoveOnly类型已删除的副本构造函数而导致错误。对于我来说,此错误意义不大,因为没有理由在此处复制任何内容而不是移动。/std:c++14,/std:c++17,/std:c++latest不帮。此外,gcc和clang正确处理代码的事实使我对msvc 2017编译器感到怀疑。
更新:
之后Yakk发现的问题是什么,我想代替使用其他容器unordered_map和代码只编译vector。