fork(4) download
  1. #include <memory>
  2. #include <string>
  3. #include <map>
  4.  
  5. template <typename> class manager;
  6.  
  7. template <typename T>
  8. class managed_resource
  9. {
  10. typedef std::shared_ptr<manager<T>> manager_ptr;
  11. public:
  12. managed_resource(manager_ptr const & parent)
  13. : parent_(parent)
  14. {
  15. }
  16.  
  17. /* ... */
  18.  
  19. private:
  20. manager_ptr parent_;
  21. };
  22.  
  23. template <typename Policy>
  24. class manager
  25. : Policy
  26. , std::enable_shared_from_this<manager<Policy>>
  27. {
  28. typedef managed_resource<Policy> resource;
  29. typedef std::shared_ptr<resource> resource_ptr;
  30. public:
  31. resource_ptr get_resource(std::string const & name)
  32. {
  33. Policy & p = *this;
  34. if(p.find(name))
  35. {
  36. return p.get(name);
  37. }
  38. resource_ptr res = std::make_shared<resource>(shared_from_this());
  39. p.store(name, res);
  40. return res;
  41. }
  42. };
  43.  
  44. class map_policy
  45. {
  46. typedef std::shared_ptr<managed_resource<map_policy>> resource_ptr;
  47. typedef std::map<std::string, resource_ptr> resources;
  48.  
  49. public:
  50. bool find(std::string const & name)
  51. {
  52. resources::iterator res_it = resources_.find(name);
  53. return res_it != resources_.end();
  54. }
  55.  
  56. resource_ptr get(std::string const & name)
  57. {
  58. resources::iterator res_it = resources_.find(name);
  59. return res_it->second;
  60. }
  61.  
  62. void store(std::string const & name, resource_ptr const & res)
  63. {
  64. resources_[name] = res;
  65. }
  66.  
  67. private:
  68. resources resources_;
  69. };
  70.  
  71. typedef manager<map_policy> my_manager;
  72.  
  73. int main()
  74. {
  75. auto m = std::make_shared<my_manager>();
  76. auto res = m->get_resource("test");
  77. return 0;
  78. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘manager< <template-parameter-1-1> >::resource_ptr manager< <template-parameter-1-1> >::get_resource(const string&)’:
prog.cpp:38:76: error: there are no arguments to ‘shared_from_this’ that depend on a template parameter, so a declaration of ‘shared_from_this’ must be available [-fpermissive]
prog.cpp:38:76: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
In file included from /usr/include/c++/4.7/bits/shared_ptr.h:52:0,
                 from /usr/include/c++/4.7/memory:87,
                 from prog.cpp:1:
/usr/include/c++/4.7/bits/shared_ptr_base.h: In instantiation of ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<manager<map_policy> >; _Args = {}; _Tp = manager<map_policy>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’:
/usr/include/c++/4.7/bits/shared_ptr.h:317:64:   required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<manager<map_policy> >; _Args = {}; _Tp = manager<map_policy>]’
/usr/include/c++/4.7/bits/shared_ptr.h:599:39:   required from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = manager<map_policy>; _Alloc = std::allocator<manager<map_policy> >; _Args = {}]’
/usr/include/c++/4.7/bits/shared_ptr.h:615:42:   required from ‘std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = manager<map_policy>; _Args = {}]’
prog.cpp:75:43:   required from here
/usr/include/c++/4.7/bits/shared_ptr_base.h:1003:4: error: ‘std::enable_shared_from_this<manager<map_policy> >’ is an inaccessible base of ‘manager<map_policy>’
stdout
Standard output is empty