c++ - 是否有在标准容器中使用 unique_ptr 的透明方法?

标签 c++ c++17 unique-ptr stdmap

是否有在容器中使用 std::unique_ptr 的透明方法?

#include <iostream>                                                                                                                         
#include <memory>                                                                                                                           
#include <map>                                                                                                                              

struct method {                                                                                                                             
    virtual ~method() { std::cout << "f\n"; };                                                                                              
};                                                                                                                                          
typedef std::unique_ptr<method> MPTR;                                                                                                       

std::map<int, MPTR> tbl;                                                                                                                    

void insert(int id, method *m) {                                                                                                            
    tbl.insert({id,std::unique_ptr<method>(m)});                                                                                            
};                                                                                                                                          

void set(int id, method *m) {                                                                                                               
    tbl[id] = std::unique_ptr<method>(m);                                                                                                   
};                                                                                                                                          

int main(int argc, char **argv) {                                                                                                           

    insert(1,new method());                                                                                                                 
    set(1,new method());                                                                                                                    
    return 0;                                                                                                                               
}   

我想使用 tbl.insert({id,m});tbl[id] = m; 等,而不必包装/为每次访问解包。

  • 是否有用于 unique_ptr 的标准容器的实现?特别是 std::map
  • 如何实现透明接口(interface)?

最佳答案

通常,我们不想隐式创建std::unique_ptr 因为that can be dangerous .

在此示例中,我建议从unique_ptr 而不是裸new 开始。这makes sure the entire lifetime is tracked .

#include <memory>
int main(int argc, char **argv) {
    auto m = std::make_unique<method>();
    insert(1, std::move(m));
}

insert 中,您还可以使用 std::move 将所有权转移到集合。

关于c++ - 是否有在标准容器中使用 unique_ptr 的透明方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54223750/

相关文章:

c++ - std::unique_ptr::reset 检查托管指针是否为空?

c++ - 如果从自定义分配器调用,则 std::cout 不输出 (Visual Studio 2019)

c++ - 正则表达式,找不到匹配项

c++ - 获取C++类成员变量全名

c++ - 将 std::filesystem 头文件添加到我的程序时编译错误

c++ - 关于 SDL_Window 和 unique_ptr 的几个问题

c++ - 带有指针和唯一指针的 emplace_back

c++ - 函数不返回我想要的值

c++ - Unresolved external 运算符(operator)删除(无效*)

c++ - 用户定义的推导指南是否涉及模板模板参数作为指南标准的模板