Skip to content

Instantly share code, notes, and snippets.

@usagi
Created April 7, 2014 06:34
Show Gist options
  • Select an option

  • Save usagi/10015677 to your computer and use it in GitHub Desktop.

Select an option

Save usagi/10015677 to your computer and use it in GitHub Desktop.
make_shared
#include <iostream>
#include <memory>
#include <stdexcept>
template <class T, class... T_params>
auto make_shared(T_params&&... params) -> std::shared_ptr<T>
{ return std::shared_ptr<T>(new T(params...)); }
class A
{
friend class B;
template <class T, class... T_params> friend auto make_shared(T_params&&... params) -> std::shared_ptr<T>;
int x = 0;
static auto operator new(unsigned long s) -> void* { std::cerr << "new " << s << "\n"; return std::malloc(s); }
A() { std::cerr << "ctor\n"; }
public:
static auto operator delete(void* p) -> void { std::cerr << "delete " << p << "\n"; return std::free(p); }
~A() { std::cerr << "dtor\n"; }
};
class B: public A
{
template <class T, class... T_params> friend auto make_shared(T_params&&... params) -> std::shared_ptr<T>;
int y = 0;
};
auto main() -> int
{
//auto a1 = A();
auto a2 = make_shared<A>();
//auto b1 = B();
auto b2 = make_shared<B>();
}
@usagi

usagi commented Apr 7, 2014

Copy link
Copy Markdown
Author

@usagi

usagi commented Apr 7, 2014

Copy link
Copy Markdown
Author

Start

new 4
ctor
new 8
ctor
dtor
delete 0x131a7f0
dtor
delete 0x131a950

0

Finish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment