fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. template <typename T>
  5. struct propagating_unique_ptr : std::unique_ptr<T> {
  6. using std::unique_ptr<T>::unique_ptr;
  7. using std::unique_ptr<T>::operator =;
  8.  
  9. const T *get() const noexcept {
  10. return std::unique_ptr<T>::get();
  11. }
  12. T *get() noexcept {
  13. return std::unique_ptr<T>::get();
  14. }
  15.  
  16. const T &operator *() const noexcept {
  17. return std::unique_ptr<T>::operator *();
  18. }
  19. T &operator *() noexcept {
  20. return std::unique_ptr<T>::operator *();
  21. }
  22.  
  23. const T *operator -> () const noexcept {
  24. return std::unique_ptr<T>::get();
  25. }
  26. T *operator -> () noexcept {
  27. return std::unique_ptr<T>::get();
  28. }
  29. };
  30.  
  31. struct foo {
  32. foo() : value_ptr_(std::make_unique<int>(3)) {}
  33. void increment() {
  34. ++(*value_ptr_); // OK
  35. }
  36. int get_value() const {
  37. return *value_ptr_;
  38. }
  39. propagating_unique_ptr<int> value_ptr_;
  40. };
  41.  
  42. int main() {
  43. foo my_foo;
  44. std::cout << my_foo.get_value() << std::endl;
  45. my_foo.increment(); // OK
  46. std::cout << my_foo.get_value() << std::endl;
  47. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
3
4