#include #include #include #include struct foo: boost::enable_shared_from_this { int x; }; void test(foo& f) { try { f.shared_from_this(); std::cout << "\tworks\n"; } catch(...) { std::cout << "\tfailed\n"; } } void test_const(foo const& f) { try { f.shared_from_this(); std::cout << "\tconst works\n"; } catch(...) { std::cout << "\tconst failed\n"; } } int main() { { foo f; std::cout << "plain foo\n"; test(f); test_const(f); std::cout << std::endl; } { boost::shared_ptr f(new foo); std::cout << "shared_ptr\n"; test(*f); test_const(*f); std::cout << std::endl; } { boost::shared_ptr f(new foo); std::cout << "shared_ptr\n"; test_const(*f); std::cout << std::endl; } { std::auto_ptr g(new foo); boost::shared_ptr f(g); std::cout << "shared_ptr from auto_ptr\n"; test(*f); test_const(*f); std::cout << std::endl; } { std::auto_ptr g(new foo); boost::shared_ptr f(g); std::cout << "shared_ptr from auto_ptr\n"; test_const(*f); std::cout << std::endl; } { std::auto_ptr g(new foo); boost::shared_ptr f(g); std::cout << "shared_ptr from auto_ptr\n"; test_const(*f); std::cout << std::endl; } return 0; }