fork download
  1. #include <boost/ptr_container/ptr_vector.hpp>
  2. #include <boost/shared_array.hpp>
  3. #include <memory>
  4. #include <vector>
  5.  
  6. #define KNOTE(fmt, args...) ::printf(fmt "\n", ##args)
  7.  
  8. // Vector of unique_ptr should be your default choice
  9. typedef std::vector<std::unique_ptr<TClass>> TClassUniqueVector;
  10.  
  11. // Vector of shared_ptr should be used if TClass is shared to others
  12. typedef std::vector<std::shared_ptr<TClass>> TClassSharedVector;
  13.  
  14. // ptr_vector may has best performance
  15. typedef boost::ptr_vector<TClass> TClassPtrVector;
  16.  
  17.  
  18. // unique_ptr can take a fix-sized array of objects
  19. typedef std::unique_ptr<TClass[]> TClassUniqueArray;
  20.  
  21. // shared_array for a fix-sized array of objects
  22. typedef boost::shared_array<TClass> TClassBoostSharedArray;
  23.  
  24. void Foo(const std::unique_ptr<TClass>& c)
  25. {
  26. c->IntValue++;
  27. }
  28.  
  29. void Foo(const std::shared_ptr<TClass>& c)
  30. {
  31. c->IntValue++;
  32. }
  33.  
  34. void TestSmartPtrMiscs()
  35. {
  36. TClassUniqueVector cv;
  37. auto uc(std::unique_ptr<TClass>(new TClass(1)));
  38. cv.push_back(std::move(uc));
  39. cv.push_back(std::unique_ptr<TClass>(new TClass(2)));
  40. Foo(cv.back());
  41.  
  42. TClassSharedVector sv;
  43. auto sc = std::make_shared<TClass>(100);
  44. sv.push_back(sc);
  45. sv.push_back(std::make_shared<TClass>(101));
  46. Foo(sv.back());
  47. }
  48.  
  49. void TestPtrVector()
  50. {
  51. TClassPtrVector pv;
  52. pv.push_back(new TClass(1));
  53. TClass* c = new TClass(2);
  54. pv.push_back(c);
  55. }
  56.  
  57. void TestSmartPtrWithArray()
  58. {
  59. {
  60. KNOTE("\nstd::unique_ptr with array");
  61. // Usage of unique_ptr for array
  62. std::unique_ptr<TClass[]> ua(new TClass[5]{1, 2, 3, 4, 5});
  63. KNOTE("Access with []: %d", ua[3].IntValue);
  64. }
  65. {
  66. KNOTE("\nboost::scoped_ptr with array is not supported");
  67. // Compile error
  68. //boost::scoped_ptr<TClass[]> s(new TClass[5]{6,7,8,9,10});
  69. }
  70. {
  71. KNOTE("\nstd::shared_ptr with array is not supported");
  72. // Compile error
  73. //std::shared_ptr<TClass[]> a(new TClass[5]{6, 7, 8, 9, 10});
  74. }
  75. {
  76. KNOTE("\nstd::shared_ptr with array shall come with custom deleter");
  77. // Usage of shared_ptr for array
  78. // Note you must supply a correct deleter for array
  79. std::shared_ptr<TClass> sa2(new TClass[5]{6, 7, 8, 9, 10},
  80. std::default_delete<TClass[]>());
  81. KNOTE("Access with .get()[]: %d", (sa2.get())[3].IntValue);
  82. }
  83. {
  84. KNOTE("\nboost::shared_ptr with array is supported");
  85. boost::shared_ptr<TClass[]> a(new TClass[5]{100, 101, 102});
  86. KNOTE("Access with []: %d", a[3].IntValue);
  87. }
  88. {
  89. KNOTE("\nboost::shared_array is an alternative");
  90. // It also can be done by boost::shared_array
  91. boost::shared_array<TClass> bsa(new TClass[5]{100, 101, 102});
  92. KNOTE("Access with []: %d", bsa[3].IntValue);
  93. }
  94. {
  95. KNOTE("\nBoth std and boost shared_ptr<T> with array T[] "
  96. "cause segment fault");
  97. // std::shared_ptr<TClass> a(new TClass[5]); // Crash
  98. // boost::shared_ptr<TClass> b(new TClass[5]); // Crash
  99. }
  100. }
  101.  
  102. int main()
  103. {
  104. TestSmartPtrMiscs();
  105. TestPtrVector();
  106. TestSmartPtrWithArray();
  107. return 0;
  108. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:46: fatal error: boost/ptr_container/ptr_vector.hpp: No such file or directory
 #include <boost/ptr_container/ptr_vector.hpp>
                                              ^
compilation terminated.
stdout
Standard output is empty