fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. struct releaseable_object
  5. {
  6. releaseable_object() : dummy_ptr(new int) {}
  7. void Release()
  8. {
  9. std::cout << "Releasing releaseable object\n";
  10. delete dummy_ptr;
  11. }
  12. int *const dummy_ptr;
  13. };
  14.  
  15. struct A : releaseable_object {};
  16. struct B : releaseable_object {};
  17.  
  18. namespace factory
  19. {
  20. A *CreateA(/* lots of parameters */) { return new A; }
  21. B *CreateB(/* lots of parameters */) { return new B; }
  22. }
  23.  
  24. template <typename T> struct Managed
  25. {
  26. using type = T;
  27. static void deleter(type *object)
  28. {
  29. std::cout << "Release!\n";
  30. object->Release();
  31. };
  32. using pointer = std::unique_ptr<T, decltype(deleter)>;
  33. };
  34.  
  35. struct ManagedA : Managed<A>
  36. {
  37. using base = Managed<A>;
  38. using base::pointer;
  39. using base::deleter;
  40. ManagedA(/* lots of parameters */) :
  41. m_pointer(nullptr, deleter)
  42. {
  43. // do A specific stuff...
  44. A *a = factory::CreateA(/* lots of parameters */);
  45. // more A specific stuff...
  46.  
  47. // wrap the pointer:
  48. m_pointer.reset(a);
  49. }
  50. pointer m_pointer;
  51. };
  52.  
  53. int main()
  54. {
  55. return 0;
  56. }
Compilation error #stdin compilation error #stdout 0s 3292KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/4.8/functional:55:0,
                 from /usr/include/c++/4.8/memory:79,
                 from prog.cpp:2:
/usr/include/c++/4.8/tuple: In instantiation of ‘struct std::_Head_base<1u, void(A*), false>’:
/usr/include/c++/4.8/tuple:229:12:   recursively required from ‘struct std::_Tuple_impl<1u, void(A*)>’
/usr/include/c++/4.8/tuple:229:12:   required from ‘struct std::_Tuple_impl<0u, A*, void(A*)>’
/usr/include/c++/4.8/tuple:521:11:   required from ‘class std::tuple<A*, void(A*)>’
/usr/include/c++/4.8/bits/unique_ptr.h:127:57:   required from ‘class std::unique_ptr<A, void(A*)>’
prog.cpp:50:10:   required from here
/usr/include/c++/4.8/tuple:172:13: error: field ‘std::_Head_base<1u, void(A*), false>::_M_head_impl’ invalidly declared function type
       _Head _M_head_impl;
             ^
/usr/include/c++/4.8/tuple: In instantiation of ‘static constexpr _Head& std::_Head_base<_Idx, _Head, false>::_M_head(std::_Head_base<_Idx, _Head, false>&) [with unsigned int _Idx = 1u; _Head = void(A*)]’:
/usr/include/c++/4.8/tuple:239:69:   required from ‘static constexpr _Head& std::_Tuple_impl<_Idx, _Head, _Tail ...>::_M_head(std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with unsigned int _Idx = 1u; _Head = void(A*); _Tail = {}]’
/usr/include/c++/4.8/tuple:743:60:   required from ‘constexpr typename std::__add_ref<_Head>::type std::__get_helper(std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with unsigned int __i = 1u; _Head = void(A*); _Tail = {}; typename std::__add_ref<_Head>::type = void (&)(A*)]’
/usr/include/c++/4.8/tuple:758:35:   required from ‘constexpr typename std::__add_ref<typename std::tuple_element<__i, std::tuple<_Elements ...> >::type>::type std::get(std::tuple<_Elements ...>&) [with unsigned int __i = 1u; _Elements = {A*, void(A*)}; typename std::__add_ref<typename std::tuple_element<__i, std::tuple<_Elements ...> >::type>::type = void (&)(A*)]’
/usr/include/c++/4.8/bits/unique_ptr.h:238:32:   required from ‘std::unique_ptr<_Tp, _Dp>::deleter_type& std::unique_ptr<_Tp, _Dp>::get_deleter() [with _Tp = A; _Dp = void(A*); std::unique_ptr<_Tp, _Dp>::deleter_type = void(A*)]’
/usr/include/c++/4.8/bits/unique_ptr.h:184:16:   required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = A; _Dp = void(A*)]’
prog.cpp:41:29:   required from here
/usr/include/c++/4.8/tuple:167:54: error: invalid initialization of reference of type ‘void (&)(A*)’ from expression of type ‘void (*)(A*)’
       _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
                                                      ^
/usr/include/c++/4.8/tuple:167:68: error: body of constexpr function ‘static constexpr _Head& std::_Head_base<_Idx, _Head, false>::_M_head(std::_Head_base<_Idx, _Head, false>&) [with unsigned int _Idx = 1u; _Head = void(A*)]’ not a return-statement
       _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
                                                                    ^
stdout

Standard output is empty