when is no constructor being called in new standards?

I made a simple experiment using:
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
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){}
};

the test was running a simple procedure
1
2
3
4
5
6
C trvalue2()
{
	B c(trvalue());
	Cout() << ",";
	return c;
}
and each time I run it I changed return-type, the type of the value being created, and I added move statement around tryvalue() and around the variable c to be returned. full source below.
the results using gcc 4.9.0 I have summarized in the table:
//                    |creatiom move   creation copy  creation move copy  creation optimized out
//return move         |20r 21r         22r 23r                            2r 21br
//return copy         |2c 22bc!                                           2 22b! 23br
//return optimized out|20 20c 21 21c   22 23 23c      22c                 
//return move copy    |                                                   22br

where 2 denotes above procedure, 20 is the same with variable c being of type C, 21,22,23 are the same with return-value being C1 C2 C3 respectively, the suffix b denotes I'm using B as the type of the variable c, r denotes I'm returning a moved value, c denotes I'm applying std::move() to tryvalue(). the ! denotes I got the copy constructor triggered twice.

quite a few surprises here, I can't detect any reliable pattern.
what should the output be according to the standard c++11 or the coming c++14?

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());
	
}

def 5
2 def ,cpy 
2r def ,mov 
2c def mov ,cpy 
20 def mov ,
20r def mov ,mov 
20c def mov ,
21 def mov ,
21r def mov ,mov 
21c def mov ,
21br def ,mov 
22 def cpy ,
22r def cpy ,mov 
22c def mov cpy ,
22b def ,cpy cpy 
22br def ,mov cpy 
22bc def mov ,cpy cpy 
23 def cpy ,
23r def cpy ,mov 
23c def cpy ,
23br def ,cpy
It's a little long, and not knowing what exactly surprised you, it's hard to know where to begin. Could you pick one particularly surprising case?

(oh, and don't forget the definition of Moveable, it's important)
Last edited on
PS: to pick the first and the last one, assuming "Moveable" is an empty struct

In "B b(trvalue());"
1. the move from trvalue's local b into trvalue's return value is subject to move/copy elision
2. copy-construction of main()'s b from trvalue()'s return value is also subject to move/copy elision
You end up with nothing except the default constructor of main's b called from within trvalue (same as you would get in C++98, actually)

The elision rules are summarized at http://en.cppreference.com/w/cpp/language/copy_elision

In "C3 c3br(trvalue23br());",
1. the move from trvalue's local b into trvalue's return value is subject to move/copy elision
2. copy-construction of trvalue23br's c from trvalue()'s return value is also subject to move/copy elision
3. using std::move in the return statement prohibits elision, but it doesn't matter here because the return type is different, so C3's constructor has to be called. C3::C3(B&& n) is called, which copy-initializes the member variable b from the lvalue n, so B's copy constructor is called.
4. copy-construction of main's c3br from the trvalue23bc's return value is subject to copy/move elision

so you end up with a "def" from trvalue default-initializing trvalue23br's c and "cpy" from C3.b's copy-initialization from n.
Last edited on
thanks, cleared up a lot of misunderstandings.

do you think any of my tests did hit https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58051

yes Moveable is empty class. as for actual examples, the interaction between copy-elision and move-construction is strange. C2 has no move-construction, C3 has no copy-construction. c3br returns copy_elision somehow, in addition to one copying. but c2br does first perform a move-construction, before also doing the copy-construction. how can a copy-constructor suddenly offer an rvalue for B? how can a return-statement with std::move trigger copy-elision in one case and not in the other?
c3br returns copy_elision somehow, in addition to one copying.

Which part is unclear?
c2br is an object, copy-initialized from a nameless temporary = elision
that nameless temporary is constructed with the constructor C3::C3(B&& n):b(n) = copy


c2br does first perform a move-construction, before also doing the copy-construction

c2br is an object, copy-initialized from a nameless temporary (elision)
that nameless temporary is constructed with the constructor C2(B n):b(n) = copy
the parameter n of that constructor is initialized with the move constructor B::B(B&&n) from the std::move(c) of the return statement = move
Last edited on
Topic archived. No new replies allowed.