fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. class Test {
  5. public:
  6. Test(){}
  7. Test(int n) : num(n) {}
  8. int num;
  9. };
  10.  
  11. template<typename T>
  12. class Object {
  13. public:
  14. Object(const T& v) : value(v) {}
  15. Object(std::unique_ptr<T>&& v) : value(*v) {}
  16.  
  17. T* operator->() { return &value; }
  18. private:
  19. T value;
  20. };
  21.  
  22. int main(int argc, char *argv[]) {
  23.  
  24. Object<Test> object1 = Test(1);
  25. std::cout << object1->num << std::endl;
  26.  
  27. Object<Test> object2 = Test();
  28. object2->num = 2;
  29. std::cout << object2->num << std::endl;
  30.  
  31. Object<Test> object3 = std::make_unique<Test>(3);
  32. std::cout << object3->num << std::endl;
  33.  
  34. Object<Test> object4 = std::make_unique<Test>();
  35. object4->num = 4;
  36. std::cout << object4->num << std::endl;
  37. }
Success #stdin #stdout 0s 4276KB
stdin
Standard input is empty
stdout
1
2
3
4