fork(1) download
  1. #include <vector>
  2. #include <utility>
  3.  
  4. template <typename T>
  5. class r_value_ref
  6. {
  7. public:
  8. explicit r_value_ref(const T& t) : t(t) {}
  9.  
  10. T& get() const
  11. {
  12. return const_cast<T&>(t);
  13. }
  14.  
  15. operator T&() const
  16. {
  17. return const_cast<T&>(t);
  18. }
  19.  
  20. private:
  21. const T& t;
  22. };
  23.  
  24. template <typename T>
  25. r_value_ref<T> my_move(const T& t)
  26. {
  27. return r_value_ref<T>(t);
  28. }
  29.  
  30. class Foo
  31. {
  32. public:
  33. Foo(r_value_ref<std::vector<int> > vec)
  34. {
  35. m_vec.swap(vec); // no .get() required !
  36. // or std::swap(vec.get(), m_vec);
  37. }
  38.  
  39. private:
  40. std::vector<int> m_vec;
  41. };
  42.  
  43. int main()
  44. {
  45. Foo foo_from_r_value(my_move(std::vector<int>(100, 1337)));
  46.  
  47. std::vector<int> v2(100, 1337);
  48. Foo foo_from_l_value(my_move(v2));
  49. }
  50.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty