//Purpose: // See how easy it is to create a dangling pointer problem. // #include "obj_id.cpp" #include #include namespace rtp = rich_typed_ptr; enum sp_enum { sp_std //smart pointers from std:: , sp_rtp //smart pointers from rtp:: }; template < sp_enum > struct test_adapt ; template < > struct test_adapt < sp_std > { template using ow_ptr = std::shared_ptr; template using wk_ptr = std::weak_ptr; static obj_id* make_obj() { return new obj_id;} template static bool empty(wk_ptrconst& wp) { return wp.expired();} template static T& deref(wk_ptrconst& wp) { auto op=wp.lock(); return *op; } }; template < > struct test_adapt < sp_rtp > { template using ow_ptr = rtp::owner_ptr; template using wk_ptr = rtp::weak_ptr; static ow_ptr make_obj() { return rtp::make();} template static bool empty(wk_ptrconst& wp) { return wp==nullptr;} template static T& deref(wk_ptrconst& wp) { return *wp;} }; template < sp_enum Sp=sp_std > struct test_run : test_adapt { using super_t = test_adapt; using super_t::make_obj; using super_t::empty; using super_t::deref; template using ow_ptr = typename super_t::template ow_ptr; template using wk_ptr = typename super_t::template wk_ptr; static void run() { std::cout<<"***test_run<"<::run()\n"; { ow_ptr outer_op(make_obj()); wk_ptr wp(outer_op); { ow_ptr inner_op(make_obj()); wp=inner_op; std::cout<<"inner empty(wp)="<::run(); test_run::run(); return 0; }