doc_how_works.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2009.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #include <boost/config.hpp>
  12. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  13. int main()
  14. {
  15. return 0;
  16. }
  17. #else
  18. #include <boost/move/detail/config_begin.hpp>
  19. //[how_works_example
  20. #include <boost/move/core.hpp>
  21. #include <iostream>
  22. class sink_tester
  23. {
  24. public: //conversions provided by BOOST_COPYABLE_AND_MOVABLE
  25. operator ::boost::rv<sink_tester>&()
  26. { return *static_cast< ::boost::rv<sink_tester>* >(this); }
  27. operator const ::boost::rv<sink_tester>&() const
  28. { return *static_cast<const ::boost::rv<sink_tester>* >(this); }
  29. };
  30. //Functions returning different r/lvalue types
  31. sink_tester rvalue() { return sink_tester(); }
  32. const sink_tester const_rvalue() { return sink_tester(); }
  33. sink_tester & lvalue() { static sink_tester lv; return lv; }
  34. const sink_tester & const_lvalue() { static const sink_tester clv = sink_tester(); return clv; }
  35. //BOOST_RV_REF overload
  36. void sink(::boost::rv<sink_tester> &) { std::cout << "non-const rvalue catched" << std::endl; }
  37. //BOOST_COPY_ASSIGN_REF overload
  38. void sink(const ::boost::rv<sink_tester> &){ std::cout << "const (r-l)value catched" << std::endl; }
  39. //Overload provided by BOOST_COPYABLE_AND_MOVABLE
  40. void sink(sink_tester &) { std::cout << "non-const lvalue catched" << std::endl; }
  41. int main()
  42. {
  43. sink(const_rvalue()); //"const (r-l)value catched"
  44. sink(const_lvalue()); //"const (r-l)value catched"
  45. sink(lvalue()); //"non-const lvalue catched"
  46. sink(rvalue()); //"non-const rvalue catched"
  47. return 0;
  48. }
  49. //]
  50. #include <boost/move/detail/config_end.hpp>
  51. #endif