fork download
  1. #include <iostream>
  2. #include <utility>
  3. using namespace std;
  4.  
  5. template <typename T>
  6. struct container
  7. {
  8. container(const T& ref) : _item(ref)
  9. {
  10. cout << "Const ref constructor" << endl;
  11. }
  12.  
  13. template <typename U>
  14. container(U&& ref) : _item(std::forward<U>(ref))
  15. {
  16. cout << "Universal ref constructor!" << endl;
  17. }
  18.  
  19. T _item;
  20. };
  21.  
  22. struct Complex
  23. {
  24. char content[10];
  25. };
  26.  
  27. struct NotMovable
  28. {
  29. NotMovable() {}
  30. NotMovable(const NotMovable&) {}
  31. NotMovable(NotMovable&&) = delete;
  32.  
  33. char content[10];
  34. };
  35.  
  36. int main() {
  37. int var = 5;
  38. container<int> intTest1(var);
  39. container<int> intTest2(std::move(var));
  40. container<int> intTest3(10);
  41.  
  42. Complex comp{};
  43. container<Complex> compTest1(comp);
  44. container<Complex> compTest2(std::move(comp));
  45. container<Complex> compTest3(Complex{});
  46.  
  47. NotMovable nm{};
  48. container<NotMovable> nmTest1(nm);
  49. // container<NotMovable> nmTest2(std::move(nm)); Won't compile
  50. // container<NotMovable> nmTest3(NotMovable{}); Won't compile, not sure why though
  51.  
  52. // your code goes here
  53. return 0;
  54. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Universal ref constructor!
Universal ref constructor!
Universal ref constructor!
Universal ref constructor!
Universal ref constructor!
Universal ref constructor!
Universal ref constructor!