fork download
  1. #include <iostream>
  2. #include <cstddef>
  3. #include <type_traits>
  4. #include <memory>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. namespace details {
  9. template<class T>
  10. struct plain_ptr_t;
  11.  
  12. //specialzation for T*
  13. template<class T>
  14. struct plain_ptr_t<T*> {
  15. T* operator()(T* t)const{return t;}
  16. };
  17.  
  18. //specialzation for std::unique_ptr
  19. template<class T, class D>
  20. struct plain_ptr_t<std::unique_ptr<T,D>> {
  21. T* operator()(std::unique_ptr<T>const& t)const{return t.get();}
  22. };
  23.  
  24. //specialzation for std::shared_ptr
  25. template<class T>
  26. struct plain_ptr_t<std::shared_ptr<T>> {
  27. T* operator()(std::shared_ptr<T>const& t)const{return t.get();}
  28. };
  29. }
  30.  
  31. struct plain_ptr {
  32. template<class T>
  33. typename std::result_of< details::plain_ptr_t<T>( T const& ) >::type
  34. operator()( T const& t ) const {
  35. return details::plain_ptr_t<T>{}( t );
  36. }
  37. };
  38.  
  39.  
  40. template<typename T>
  41. class VectorT : std::vector<T> {
  42. using vec = std::vector<T>;
  43.  
  44. public:
  45. using vec::at;
  46. using vec::emplace_back;
  47. using vec::push_back;
  48.  
  49. typename std::result_of< plain_ptr(typename vec::value_type const&)>::type
  50. operator[](const size_t _Pos) const {
  51. return plain_ptr{}(at(_Pos));
  52. }
  53. };
  54.  
  55.  
  56.  
  57. int main() {
  58.  
  59. VectorT<std::unique_ptr<int>> uptr_v;
  60. uptr_v.emplace_back(std::make_unique<int>(1));
  61. int* p1 = uptr_v[0];
  62. std::cout << *p1;
  63.  
  64. VectorT<int*> v;
  65. v.emplace_back(p1);
  66. int *p2 = v[0];
  67. std::cout << *p2;
  68.  
  69. // your code goes here
  70. return 0;
  71. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
11