Is there a transparent way of using unique_ptr in std containers?

  • A+
Category:Languages

Is there a transparent way of using std::unique_ptr in containers?

#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;                                                                                                                                }    

I'd like to use tbl.insert({id,m}); and tbl[id] = m; etc. instead of having to wrap/unwrap for each access.

  • Are there implementations of std containers for unique_ptr? In particular std::map.
  • How would a transparent interface be implemented?

 


When you pass a raw pointer into a function, it is generally unknown who is expected to keep ownership of the pointed-to object when the function exits - the caller or the callee. There is nothing in the language to specify or enforce that.

So, it is generally good practice to pass in a raw pointer only when the callee reads or modifies the object but the caller is expected to maintain ownership.

In your example, that is not the case. You want your functions to take ownership of the allocated method objects. So, you should change your functions to pass in std::unique_ptr<method> objects by value, instead of passing raw method* pointers. That makes it very explicit that ownership is expected to pass from the caller to the callee, eg:

#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, MPTR m) {     tbl.insert(std::make_pair(id, std::move(m))); };  void set(int id, MPTR m) {     tbl[id] = std::move(m); };  int main() {     insert(1, MPTR(new method)); // or insert(1, std:::make_unique<method>()) in C++14 and later     set(1, MPTR(new method)); // or set(1, std:::make_unique<method>()) in C++14 and later     return 0;                                                                                                                                } 

Live demo

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: