小编stb*_*ody的帖子

vector<map<move-only type>> 无法使用 MSVC 进行编译

制作仅移动类型的地图向量似乎在 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 …
Run Code Online (Sandbox Code Playgroud)

c++ vector visual-c++ move-semantics

6
推荐指数
1
解决办法
432
查看次数

标签 统计

c++ ×1

move-semantics ×1

vector ×1

visual-c++ ×1