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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
struct B : Moveable<B>{//has verbose copy and move constructor
int val;
B():val(0){cout<<"def ";}
B(B &&n):val(n.val+2){cout<<"mov ";}
B(const B &n):val(n.val+1){cout<<"cpy ";}
};
B trvalue()
{
B b;
b.val=5;
return b;
}
struct C{//move is move-constructing explicitely
B b;
C(){}
C(B &&n):b(std::move(n)){}
C(const B& n):b(n){}
};
struct C1{//same but no copy constructor exists
B b;
C1(){}
C1(B &&n):b(std::move(n)){}
//C1(B n):b(n){}
};
struct C2{//no move constructor exists, copy-constructor is creating a new object for parameter
B b;
C2(){}
//C2(B &&n):b(std::move(n)){}
C2(B n):b(n){}
};
struct C3{//move constructor doesn't pass on the move, no copy constructor
B b;
C3(){}
C3(B &&n):b(n){}
//C3(B n):b(n){}
};
C trvalue2()
{
B c(trvalue());
cout << ",";
return c;
//C has only constructor taking a copy: def cpy cpy
//C with proper copy-constructor: def ,cpy
}
C trvalue2r()
{
B c(trvalue());
cout << ",";
return std::move(c);
//2r def ,mov
}
C trvalue2c()
{
B c(std::move(trvalue()));
cout << ",";
return c;
//C has only constructor taking a copy: 2c def mov cpy cpy
//C with proper copy-constructor: 2c def mov ,cpy
}
C trvalue20()
{
C c(trvalue());
cout << ",";
return c;
//20 def mov ,
}
C trvalue20r()
{
C c(trvalue());
cout << ",";
return std::move(c);
//20r def mov ,mov
}
C trvalue20c()
{
C c(std::move(trvalue()));
cout << ",";
return c;
//20c def mov ,
}
C1 trvalue21()
{
C1 c(trvalue());
cout << ",";
return c;
//21 def mov ,
}
C2 trvalue22()
{
C2 c(trvalue());
cout << ",";
return c;
//22 def cpy ,
}
C3 trvalue23()
{
C3 c(trvalue());
cout << ",";
return c;
//23 def cpy ,
}
C1 trvalue21r()
{
C1 c(trvalue());
cout << ",";
return std::move(c);
//21r def mov ,mov
}
C2 trvalue22r()
{
C2 c(trvalue());
cout << ",";
return std::move(c);
//22r def cpy ,mov
}
C3 trvalue23r()
{
C3 c(trvalue());
cout << ",";
return std::move(c);
//23r def cpy ,mov
}
C1 trvalue21c()
{
C1 c(std::move(trvalue()));
cout << ",";
return c;
//21c def mov ,
}
C2 trvalue22c()
{
C2 c(std::move(trvalue()));
cout << ",";
return c;
//22c def mov cpy ,
}
C3 trvalue23c()
{
C3 c(std::move(trvalue()));
cout << ",";
return c;
//23c def cpy ,
}
//C1 trvalue21b()
//{
// B c(trvalue());
// return c;
//}
C2 trvalue22b()
{
B c(trvalue());
cout << ",";
return c;
//22b def ,cpy cpy
}
//C3 trvalue23b()
//{
// B c(trvalue());
// return c;
//}
C1 trvalue21br()
{
B c(trvalue());
cout << ",";
return std::move(c);
//21br def ,mov
}
C2 trvalue22br()
{
B c(trvalue());
cout << ",";
return std::move(c);
//22br def ,mov cpy
}
C3 trvalue23br()
{
B c(trvalue());
cout << ",";
return std::move(c);
//23br def ,cpy
}
//C1 trvalue21bc()
//{
// B c(std::move(trvalue()));
// return c;
//}
C2 trvalue22bc()
{
B c(std::move(trvalue()));
cout << ",";
return c;
//22bc def mov ,cpy cpy
}
//C3 trvalue23bc()
//{
// B c(std::move(trvalue()));
// return c;
//}
int main()
{
B b(trvalue());
cout<<b.val<<EOL<<"2 ";
C c(trvalue2());
cout<<EOL<<"2r ";
C cr(trvalue2r());
cout<<EOL<<"2c ";
C cc(trvalue2c());
cout<<EOL<<"20 ";
C c0(trvalue20());
cout<<EOL<<"20r ";
C c0r(trvalue20r());
cout<<EOL<<"20c ";
C c0c(trvalue20c());
cout<<EOL<<"21 ";
C1 c1(trvalue21());
cout<<EOL<<"21r ";
C1 c1r(trvalue21r());
cout<<EOL<<"21c ";
C1 c1c(trvalue21c());
//cout<<EOL<<"21b ";
//C1 c1b(trvalue21b());
cout<<EOL<<"21br ";
C1 c1br(trvalue21br());
//cout<<EOL<<"21bc ";
//C1 c1bc(trvalue21bc());
cout<<EOL<<"22 ";
C2 c2(trvalue22());
cout<<EOL<<"22r ";
C2 c2r(trvalue22r());
cout<<EOL<<"22c ";
C2 c2c(trvalue22c());
cout<<EOL<<"22b ";
C2 c2b(trvalue22b());
cout<<EOL<<"22br ";
C2 c2br(trvalue22br());
cout<<EOL<<"22bc ";
C2 c2bc(trvalue22bc());
cout<<EOL<<"23 ";
C3 c3(trvalue23());
cout<<EOL<<"23r ";
C3 c3r(trvalue23r());
cout<<EOL<<"23c ";
C3 c3c(trvalue23c());
//cout<<EOL<<"23b ";
//C3 c3b(trvalue23b());
cout<<EOL<<"23br ";
C3 c3br(trvalue23br());
//cout<<EOL<<"23bc ";
//C3 c3bc(trvalue23bc());
}
|