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?