fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. #include <typeinfo>
  5.  
  6. // <static_type_info_extraction purpose = "debugging" from = "http://stackoverflow.com/a/20170989/2613882">
  7.  
  8. #include <cstddef>
  9. #include <stdexcept>
  10. #include <cstring>
  11. #include <ostream>
  12.  
  13. #ifndef _MSC_VER
  14. # if __cplusplus < 201103
  15. # define CONSTEXPR11_TN
  16. # define CONSTEXPR14_TN
  17. # define NOEXCEPT_TN
  18. # elif __cplusplus < 201402
  19. # define CONSTEXPR11_TN constexpr
  20. # define CONSTEXPR14_TN
  21. # define NOEXCEPT_TN noexcept
  22. # else
  23. # define CONSTEXPR11_TN constexpr
  24. # define CONSTEXPR14_TN constexpr
  25. # define NOEXCEPT_TN noexcept
  26. # endif
  27. #else // _MSC_VER
  28. # if _MSC_VER < 1900
  29. # define CONSTEXPR11_TN
  30. # define CONSTEXPR14_TN
  31. # define NOEXCEPT_TN
  32. # elif _MSC_VER < 2000
  33. # define CONSTEXPR11_TN constexpr
  34. # define CONSTEXPR14_TN
  35. # define NOEXCEPT_TN noexcept
  36. # else
  37. # define CONSTEXPR11_TN constexpr
  38. # define CONSTEXPR14_TN constexpr
  39. # define NOEXCEPT_TN noexcept
  40. # endif
  41. #endif // _MSC_VER
  42.  
  43. class static_string
  44. {
  45. const char* const p_;
  46. const std::size_t sz_;
  47.  
  48. public:
  49. typedef const char* const_iterator;
  50.  
  51. template <std::size_t N>
  52. CONSTEXPR11_TN static_string(const char(&a)[N]) NOEXCEPT_TN
  53. : p_(a)
  54. , sz_(N-1)
  55. {}
  56.  
  57. CONSTEXPR11_TN static_string(const char* p, std::size_t N) NOEXCEPT_TN
  58. : p_(p)
  59. , sz_(N)
  60. {}
  61.  
  62. CONSTEXPR11_TN const char* data() const NOEXCEPT_TN {return p_;}
  63. CONSTEXPR11_TN std::size_t size() const NOEXCEPT_TN {return sz_;}
  64.  
  65. CONSTEXPR11_TN const_iterator begin() const NOEXCEPT_TN {return p_;}
  66. CONSTEXPR11_TN const_iterator end() const NOEXCEPT_TN {return p_ + sz_;}
  67.  
  68. CONSTEXPR11_TN char operator[](std::size_t n) const
  69. {
  70. return n < sz_ ? p_[n] : throw std::out_of_range("static_string");
  71. }
  72. };
  73.  
  74. inline
  75. std::ostream&
  76. operator<<(std::ostream& os, static_string const& s)
  77. {
  78. return os.write(s.data(), s.size());
  79. }
  80.  
  81. template <class T>
  82. CONSTEXPR14_TN
  83. static_string
  84. type_name()
  85. {
  86. #ifdef __clang__
  87. static_string p = __PRETTY_FUNCTION__;
  88. return static_string(p.data() + 31, p.size() - 31 - 1);
  89. #elif defined(__GNUC__)
  90. static_string p = __PRETTY_FUNCTION__;
  91. # if __cplusplus < 201402
  92. return static_string(p.data() + 36, p.size() - 36 - 1);
  93. # else
  94. return static_string(p.data() + 46, p.size() - 46 - 1);
  95. # endif
  96. #elif defined(_MSC_VER)
  97. static_string p = __FUNCSIG__;
  98. return static_string(p.data() + 38, p.size() - 38 - 7);
  99. #endif
  100. }
  101. // </static_type_info_extraction>
  102.  
  103. struct _GError
  104. {
  105. int domain;
  106. int code;
  107. char *message;
  108. };
  109.  
  110. typedef struct _GError GError;
  111.  
  112. void my_deleter(GError*p){std::cout<<"0";delete p;}
  113.  
  114. template<class C> class MySmartPtr: public std::shared_ptr<C>
  115. {
  116. public:
  117. typedef std::shared_ptr<C> _base;
  118. typedef std::function<void(C*)> _Fn;
  119.  
  120. MySmartPtr(C*elem, _Fn fn)
  121. :_base(elem, _Fn([fn](C*elem){std::cout <<"2";if(elem) fn(elem);}))
  122. {}
  123.  
  124. };
  125.  
  126. int main() {
  127.  
  128. std::function<void(GError*)> my_d = my_deleter;
  129. auto ptr = MySmartPtr<GError>(nullptr, my_d);
  130. auto my_dd = std::get_deleter<MySmartPtr<GError>::_Fn>(ptr);
  131.  
  132. std::cout << typeid(my_dd).name() << '\n';
  133. std::cout<<type_name<decltype(my_dd)>() << '\n';
  134.  
  135. std::cout << "my_dd " << (my_dd?"not empty":"empty") << '\n';
  136.  
  137. if(my_dd)
  138. {
  139. auto p = new GError;
  140. (*my_dd)(p);
  141. }
  142.  
  143. return 0;
  144. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
PSt8functionIFvP7_GErrorEE
std::function<void (_GError *)> *
my_dd not empty
202