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

时间:2017-02-07 15:26:58

标签: c++ c++11 gcc

我正在尝试编译google的稀疏哈希映射,但是有一个错误。该错误消息似乎与使用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;

1 个答案:

答案 0 :(得分:1)

我认为根源存在问题。

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

考虑容器,这意味着如果有人要求资源,地图无法保存您指向的对象的副本,使地图本身无效(即:该条目在其后面将具有无效的唯一指针)首先“得到”它)

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

相关问题