无法编译 google::sparse_hash_map,其值为 unique_ptr

fail to compile google::sparse_hash_map with value as unique_ptr

本文关键字:ptr unique hash 编译 google sparse map      更新时间:2023-10-16

我正在尝试编译谷歌的稀疏哈希图,但有一个错误。错误消息似乎与使用 unique_ptr 有关。删除unique_ptr后编译成功。

  google::sparse_hash_map<std::string, std::unique_ptr<int>> testers_;

错误消息如下所示。有什么建议吗?多谢!

build/libgcc/include/c++/trunk/bits/stl_pair.h:127:17: note: explicitly defaulted function was implicitly deleted here
  constexpr pair(const pair&) = default;
            ^
build/libgcc/include/c++/trunk/bits/stl_pair.h:102:11: note: copy constructor of 'pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char>, std::basic_string<char> >, std::unique_ptr<int, std::default_delete<int> > >' is implicitly deleted because field 'second' has a deleted copy constructor
  _T2 second;                /// @c second is a copy of the second object
      ^
/build/libgcc/include/c++/trunk/bits/unique_ptr.h:356:7: note: 'unique_ptr' has been explicitly marked deleted here
  unique_ptr(const unique_ptr&) = delete;

我认为根源有问题。

unique_ptr的意图是指向特定资源的指针只有一个所有者。检索unique_ptr副本的最后一个。

考虑一个容器,这意味着如果有人请求资源,则映射无法容纳您所指向的对象的副本,从而使映射本身无效(即:在第一个"get"之后,该条目内部将有一个无效的唯一指针(

我相信一些 Cx11 约束禁止编译避免错误。