Skip to content

Instantly share code, notes, and snippets.

@bitdewy
Created April 15, 2014 03:48
Show Gist options
  • Save bitdewy/10700518 to your computer and use it in GitHub Desktop.
Save bitdewy/10700518 to your computer and use it in GitHub Desktop.
bo. make unique with deleter
#include <memory>
#include <iostream>
namespace bo {
template<typename T, typename Arg, typename Deleter = std::default_delete<T>>
std::unique_ptr<T, Deleter> make_unique(Arg&& a, Deleter&& d = std::default_delete<T>()) {
return std::unique_ptr<T, Deleter>(new T(std::forward<Arg>(a)), std::forward<Deleter>(d));
}
}
class Bo {
public:
Bo(int i) : i_(i) {}
~Bo() { std::cout << "dtor ..." << std::endl; }
void hello() const { std::cout << i_ << std::endl; }
private:
int i_;
};
template <typename T>
struct deleter {
deleter() {}
void operator()(T* ptr) const {
std::cout << "deleting ..." << std::endl;
delete ptr;
}
};
int main() {
{
const auto& b = bo::make_unique<Bo>(8, deleter<Bo>());
b->hello();
}
std::cout << "out of scope" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment