fork download
  1. template<typename T> class value_ptr {
  2. struct Base {
  3. virtual T& get() = 0;
  4. virtual const T& get() const = 0;
  5. virtual ~Base() {}
  6. virtual std::unique_ptr<Base> clone() const = 0;
  7. };
  8. template<typename Der> struct Derived : public Base {
  9. Der d;
  10. template<typename... args> Derived(args...&& arg)
  11. : d(std::forward<args>(arg)...) {}
  12. // Plus copy/move constructor
  13. virtual T& get() { return d; }
  14. virtual const T& get() const { return d; }
  15. virtual std::unique_ptr<Base> clone() const {
  16. return make_unique<Derived>(d);
  17. }
  18. };
  19. std::unique_ptr<Base> base;
  20. public:
  21. value_ptr() = delete;
  22. value_ptr(const value_ptr& other)
  23. : base(other.base->clone()) {}
  24. value_ptr(value_ptr&& other)
  25. : base(std::move(other.base)) {}
  26. template<typename Der> value_ptr(Der t) {
  27. base = make_unique<Derived<Der>(std::move(t));
  28. }
  29.  
  30. T* operator->() { return base->get(); }
  31. const T* operator->() const { return base->get(); }
  32. T& operator*() { return *base->get(); }
  33. const T& operator*() const { return *base->get(); }
  34.  
  35. void swap(value_ptr& other) {
  36. base.swap(other.base);
  37. }
  38.  
  39. value_ptr& operator=(value_ptr other) {
  40. this->swap(other);
  41. }
  42. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6:17: error: 'unique_ptr' in namespace 'std' does not name a type
prog.cpp:10:51: error: expected ',' or '...' before '&&' token
prog.cpp:15:17: error: 'unique_ptr' in namespace 'std' does not name a type
prog.cpp:19:5: error: 'unique_ptr' in namespace 'std' does not name a type
prog.cpp: In constructor 'value_ptr<T>::Derived<Der>::Derived(args ...)':
prog.cpp:11:17: error: 'forward' is not a member of 'std'
prog.cpp:11:34: error: expected primary-expression before '>' token
prog.cpp:11:36: error: 'arg' was not declared in this scope
prog.cpp: In copy constructor 'value_ptr<T>::value_ptr(const value_ptr<T>&)':
prog.cpp:23:11: error: class 'value_ptr<T>' does not have any field named 'base'
prog.cpp: In constructor 'value_ptr<T>::value_ptr(value_ptr<T>&&)':
prog.cpp:25:11: error: class 'value_ptr<T>' does not have any field named 'base'
prog.cpp:25:16: error: 'move' is not a member of 'std'
prog.cpp: In constructor 'value_ptr<T>::value_ptr(Der)':
prog.cpp:27:9: error: 'base' was not declared in this scope
prog.cpp:27:16: error: 'make_unique' was not declared in this scope
prog.cpp:27:41: error: 'move' is not a member of 'std'
prog.cpp: In member function 'T* value_ptr<T>::operator->()':
prog.cpp:30:30: error: 'base' was not declared in this scope
prog.cpp: In member function 'const T* value_ptr<T>::operator->() const':
prog.cpp:31:42: error: 'base' was not declared in this scope
prog.cpp: In member function 'T& value_ptr<T>::operator*()':
prog.cpp:32:30: error: 'base' was not declared in this scope
prog.cpp: In member function 'const T& value_ptr<T>::operator*() const':
prog.cpp:33:42: error: 'base' was not declared in this scope
prog.cpp: In member function 'void value_ptr<T>::swap(value_ptr<T>&)':
prog.cpp:36:9: error: 'base' was not declared in this scope
stdout
Standard output is empty