对仅移动类型的映射进行矢量处理似乎在Windows上无法正常工作。在此处查看代码:https://godbolt.org/z/yAHmzh
#include <vector>
#include <map>
#include <memory>
// vector<vector<move-only>> works
void foo() {
std::vector<std::vector<std::unique_ptr<int>>> outer;
std::vector<std::unique_ptr<int>> inner;
std::unique_ptr<int> p = std::make_unique<int>(1);
inner.push_back(std::move(p));
outer.push_back(std::move(inner));
}
// vector<map<move-only>> fails to compile upon inserting an element.
void bar() {
std::vector<std::map<std::unique_ptr<int>, std::unique_ptr<int>>> vec;
std::map<std::unique_ptr<int>, std::unique_ptr<int>> map;
std::unique_ptr<int> p1 = std::make_unique<int>(1);
std::unique_ptr<int> p2 = std::make_unique<int>(2);
map.insert(std::make_pair(std::move(p1), std::move(p2)));
// The following line fails to compile on windows. It errors with a message about
// the unique_ptr copy constructor being explicitly deleted. This seems to only happen
// on windows. GCC and clang have no problem with this.
vec.push_back(std::move(map));
}
int main(int argv, char** argc)
{
foo();
bar();
}
GCC和Clang对该代码没有问题,但是MSVC无法编译。
正在寻找一种解决方法,使我能够在所有主要编译器上进行编译。
std::map
的move构造函数未由标准定义为noexcept
。因此,std::vector
退回到使用复制构造函数(例如,通过使用std::move_if_noexcept
)。
就是说,允许编译器将函数标记为noexcept
,与标准是否强制执行无关。这可能是GCC和Clang(它们使用的库)所做的。
您会注意到std::vector<std::list<std::unique_ptr<int>>>
(可能还有其他情况)的情况相同。