File: doc_construct_forward.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (109 lines) | stat: -rw-r--r-- 4,106 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright David Abrahams, Vicente Botet, Ion Gaztanaga 2009.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/move for documentation.
//
//////////////////////////////////////////////////////////////////////////////

#include <boost/move/detail/config_begin.hpp>

//[construct_forward_example
#include <boost/move/utility_core.hpp>
#include <iostream>

class copyable_only_tester
{
   public:
   copyable_only_tester()
   {  std::cout << "copyable_only_tester()" << std::endl;   }

   copyable_only_tester(const copyable_only_tester&)
   {  std::cout << "copyable_only_tester(const copyable_only_tester&)" << std::endl;   }

   copyable_only_tester(int)
   {  std::cout << "copyable_only_tester(int)" << std::endl;   }

   copyable_only_tester(int, double)
   {  std::cout << "copyable_only_tester(int, double)" << std::endl;   }
};

class copyable_movable_tester
{
   // move semantics
   BOOST_COPYABLE_AND_MOVABLE(copyable_movable_tester)
   public:

   copyable_movable_tester()
   {  std::cout << "copyable_movable_tester()" << std::endl;   }

   copyable_movable_tester(int)
   {  std::cout << "copyable_movable_tester(int)" << std::endl;   }

   copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester))
   {  std::cout << "copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester))" << std::endl;   }

   copyable_movable_tester(const copyable_movable_tester &)
   {  std::cout << "copyable_movable_tester(const copyable_movable_tester &)" << std::endl;   }

   copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester), BOOST_RV_REF(copyable_movable_tester))
   {  std::cout << "copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester), BOOST_RV_REF(copyable_movable_tester))" << std::endl;   }

   copyable_movable_tester &operator=(BOOST_RV_REF(copyable_movable_tester))
   {  std::cout << "copyable_movable_tester & operator=(BOOST_RV_REF(copyable_movable_tester))" << std::endl; 
      return *this;  }

   copyable_movable_tester &operator=(BOOST_COPY_ASSIGN_REF(copyable_movable_tester))
   {  std::cout << "copyable_movable_tester & operator=(BOOST_COPY_ASSIGN_REF(copyable_movable_tester))" << std::endl;
      return *this;  }
};

//1 argument
template<class MaybeMovable, class MaybeRv>
void function_construct(BOOST_FWD_REF(MaybeRv) x)
{  MaybeMovable m(boost::forward<MaybeRv>(x));   }

//2 argument
template<class MaybeMovable, class MaybeRv, class MaybeRv2>
void function_construct(BOOST_FWD_REF(MaybeRv) x, BOOST_FWD_REF(MaybeRv2) x2)
{  MaybeMovable m(boost::forward<MaybeRv>(x), boost::forward<MaybeRv2>(x2));  }

int main()
{
   copyable_movable_tester m;
   //move constructor
   function_construct<copyable_movable_tester>(boost::move(m));
   //copy constructor
   function_construct<copyable_movable_tester>(copyable_movable_tester());
   //two rvalue constructor
   function_construct<copyable_movable_tester>(boost::move(m), boost::move(m));

   copyable_only_tester nm;
   //copy constructor (copyable_only_tester has no move ctor.)
   function_construct<copyable_only_tester>(boost::move(nm));
   //copy constructor
   function_construct<copyable_only_tester>(nm);
   //int constructor
   function_construct<copyable_only_tester>(int(0));
   //int, double constructor
   function_construct<copyable_only_tester>(int(0), double(0.0));

   //Output is:
   //copyable_movable_tester()
   //copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester))
   //copyable_movable_tester()
   //copyable_movable_tester(const copyable_movable_tester &)
   //copyable_movable_tester(BOOST_RV_REF(copyable_movable_tester), BOOST_RV_REF(copyable_movable_tester))
   //copyable_only_tester()
   //copyable_only_tester(const copyable_only_tester&)
   //copyable_only_tester(const copyable_only_tester&)
   //copyable_only_tester(int)
   //copyable_only_tester(int, double)
   return 0;
}
//]

#include <boost/move/detail/config_end.hpp>