fork download
  1. #include <memory>
  2.  
  3. template<class T>
  4. struct call_drop {
  5. void operator()(T* p) const {p->drop();}
  6. };
  7.  
  8. template<class T, class U>
  9. std::unique_ptr<T, call_drop<T>> make_drop_unique(U&& obj)
  10. {return std::unique_ptr<T, call_drop<T>>(new T(std::forward<U>(obj)));}
  11. template<class T>
  12. std::unique_ptr<T, call_drop<T>> make_drop_unique()
  13. {return std::unique_ptr<T, call_drop<T>>(new T());}
  14.  
  15. struct thingy {
  16. void drop(){};
  17. };
  18.  
  19. int main() {
  20. auto ptr1 = make_drop_unique<thingy>(thingy());
  21. auto ptr2 = make_drop_unique<thingy>();
  22. }
Success #stdin #stdout 0s 3056KB
stdin
Standard input is empty
stdout
Standard output is empty