Advertisement
Guest User

Proving that std::move constructor can be used "indirectly"

a guest
Apr 4th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <UnitTest11.hpp>
  2.  
  3. struct Moveable
  4. {
  5.         Moveable()
  6.                 : wasCopyCalled(false), wasMoveCalled(false)
  7.         {
  8.         }
  9.  
  10.         Moveable(const Moveable& orig)
  11.                 : wasCopyCalled(true), wasMoveCalled(false)
  12.         {
  13.         }
  14.  
  15.         Moveable(Moveable&& orig)
  16.                 : wasCopyCalled(false), wasMoveCalled(true)
  17.         {
  18.         }
  19.  
  20.         bool wasCopyCalled;
  21.         bool wasMoveCalled;
  22. };
  23.  
  24. class CreatedFromMove
  25. {
  26. public:
  27.     CreatedFromMove(Moveable moveable)
  28.         : m_moveable(moveable)
  29.     {
  30.             AssertThat(moveable.wasCopyCalled, ut11::Is::False);
  31.             AssertThat(moveable.wasMoveCalled, ut11::Is::True);
  32.     }
  33.  
  34. private:
  35.     Moveable m_moveable;
  36. };
  37.  
  38. class CreatedFromCopy
  39. {
  40. public:
  41.     CreatedFromCopy(Moveable moveable)
  42.         : m_moveable(moveable)
  43.     {
  44.         AssertThat(moveable.wasCopyCalled, ut11::Is::True);
  45.         AssertThat(moveable.wasMoveCalled, ut11::Is::False);
  46.     }
  47.  
  48. private:
  49.     Moveable m_moveable;
  50. };
  51.  
  52. class MoveTests : public ut11::TestFixture
  53. {
  54. public:
  55.  
  56.         virtual void Run()
  57.         {
  58.                 Then("attempting the movement results in the movement constructor being used", [&]() {
  59.  
  60.                         Moveable testObject;
  61.                         CreatedFromMove created(std::move(testObject));
  62.                 });
  63.  
  64.                 Then("attempting the copy results in the copy constructor being used", [&]() {
  65.  
  66.                         Moveable testObject;
  67.                         CreatedFromCopy created(testObject);
  68.                 });
  69.         }
  70. };
  71. DeclareFixture(MoveTests);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement