0

见这里:https ://godbolt.org/z/MW3jW6

使用 gcc 8.4(godbolt 中的 8.3)我的代码无法编译。使用 gcc 10.x 似乎可以编译。但我无法弄清楚错误到底告诉我什么......

这是供参考的代码:

#include <mutex>
#include <condition_variable>
#include <iostream>
#include <unordered_map>

class non_copy
{
public:
    non_copy() = default;
    non_copy(non_copy&&){}
    non_copy(non_copy&) = delete;
private:
    std::mutex m;
    std::condition_variable cv;
};

class item
{
public:
    item() = default;
    item(item&&){}
private:
    non_copy  m_item; // Works if I remove this
};

bool test()
{
    // does not work
    std::unordered_map<int, item> map; // Error here
    map.emplace(1, item());

    // Works ok
    std::unordered_map<int, std::string> map2;
    map2.emplace(1, "test");
    return true; 
}

int main()
{
    std::cout << "test" << std::endl;
    return test();
}

如果我non_coom m_itemitem类中删除它,那么它会编译。我想我只需要创建移动构造函数,但这还不够。map.emplace - 所以我读过 - 应该将右值引用(移动 c'tor)作为参数。所以我想我在这里有点困惑。

我想要做的只是将 new items 插入到地图中 - 我不关心处理内容 - 所以互斥锁和条件 var 将只是默认构造的。

注意:查看我在链接中得到的错误 - 它非常大,但如果需要,我可以将其复制到此处

4

1 回答 1

1

您的复制构造函数(带有const),即使合法是不寻常的,并且似乎有问题。

使用常规的:

non_copy(const non_copy&) = delete;
于 2020-11-02T17:02:32.693 回答