无法编译 google::sparse_hash_map,其值为 unique_ptr
fail to compile google::sparse_hash_map with value as unique_ptr
我正在尝试编译谷歌的稀疏哈希图,但有一个错误。错误消息似乎与使用 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 约束禁止编译避免错误。
相关文章:
- 为什么 std::unique 不调用 std::sort?
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 生成"unique"矩阵
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 我对 std::unique(算法)C++有问题
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- std::shared_ptr::unique(),复制和线程安全
- 如何在C++03中用自定义谓词调用std::unique
- C++中的指针否定 (!ptr == NULL)
- C++14 unique_ptr并使用已删除的函数'std::unique-ptr' unique_ptr错误