-
Notifications
You must be signed in to change notification settings - Fork 11
/
insert_vs_emplace.html
815 lines (728 loc) · 22.5 KB
/
insert_vs_emplace.html
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>insert vs emplace</title>
<style>
p {text-align:justify}
li {text-align:justify}
blockquote.note
{
background-color:#E0E0E0;
padding-left: 15px;
padding-right: 15px;
padding-top: 1px;
padding-bottom: 1px;
}
ins {color:#00A000}
del {color:#A00000}
</style>
</head>
<body>
<address align=right>
<br/>
<br/>
<a href="mailto:[email protected]">Howard E. Hinnant</a><br/>
2014-07-28<br/>
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/">
<img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by/4.0/80x15.png" /></a><br />
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.
</address>
<hr/>
<h1 align=center><code>insert</code> vs <code>emplace</code></h1>
<h2>Contents</h2>
<ul>
<li><a href="#Introduction">Introduction</a></li>
<li><a href="#Experiment">The Experiment</a></li>
<li><a href="#Results">The Results</a>
<ul>
<li><a href="#lvalue_no_realloc">lvalue with no reallocation</a></li>
<li><a href="#xvalue_no_realloc">xvalue with no reallocation</a></li>
<li><a href="#prvalue_no_realloc">prvalue with no reallocation</a></li>
<li><a href="#lvalue_realloc">lvalue with reallocation</a></li>
<li><a href="#xvalue_realloc">xvalue with reallocation</a></li>
<li><a href="#prvalue_realloc">prvalue with reallocation</a></li>
</ul>
</li>
<li><a href="#Summary">Summary</a></li>
<li><a href="#Appendix">Appendix: The test code</a></li>
<li><a href="#Acknowledgments">Acknowledgments</a></li>
</ul>
<a name="Introduction"></a><h2>Introduction</h2>
<p>
C++11 brings a new member function to the sequence containers:
</p>
<blockquote><pre>
template <class... Args> iterator emplace(const_iterator position, Args&&... args);
</pre></blockquote>
<p>
This new function is quite similar to these <code>insert</code> members:
</p>
<blockquote><pre>
iterator insert(const_iterator position, const value_type& x);
iterator insert(const_iterator position, value_type&& x);
</pre></blockquote>
<p>
The <code>insert</code> members can be handily used to insert a new
<code>value_type</code> into a container. But with <code>emplace</code> one
can directly construct the new <code>value_type</code> into the container
using <i>any</i> of <code>value_type</code>'s accessible constructors.
For example given an example class <code>X</code>:
</p>
<blockquote><pre>
class X
{
int i_;
int* p_;
public:
~X();
X(const X& x);
X& operator=(const X& x);
X(X&& x) noexcept;
X& operator=(X&& x) noexcept;
X(int i, int* p);
};
</pre></blockquote>
<p>
One can use <code>insert</code> to place an <code>x</code> into a
<code>vector<X></code> (for example) like this:
</p>
<blockquote><pre>
v.insert(v.begin(), x);
v.insert(v.begin(), std::move(x));
</pre></blockquote>
<p>
But with <code>emplace</code> one can also use the <code>X(int i, int* p)</code>
constructor directly:
</p>
<blockquote><pre>
v.emplace(v.begin(), 3, nullptr);
</pre></blockquote>
<p>
So far so good. As it turns out, you can also use <code>emplace</code> to
insert an <code>x</code>:
</p>
<blockquote><pre>
v.emplace(v.begin(), x);
</pre></blockquote>
<p>
This would simply use <code>X</code>'s copy constructor (just like
<code>insert</code> would do). So the question arises:
</p>
<blockquote class="note">
<p>
When one has a choice where either <code>insert</code> or <code>emplace</code>
would do, which one should you choose?
</p>
</blockquote>
<p>
This question is what this paper attempts to answer.
</p>
<a name="Experiment"></a><h2>The Experiment</h2>
<p>
To create a measurement, the example class <code>X</code> above is instrumented
so that one can count how many times each member is called, and those
measurements are monitored while <code>insert</code>ing / <code>emplace</code>ing
an <code>X</code> into a <code>vector<X></code>. The container
<code>vector</code> was chosen because this is the most important container
in the standard library, and because on this container, it makes a difference.
For node-based containers, no difference between <code>insert</code> and
<code>emplace</code> would be measured.
</p>
<p>
Additionally, whether the inserted <code>x</code> is an lvalue, xvalue, or
prvalue can make a difference as well. And so measurements are taken for all
three possibilities. Also it can make a difference if the insertion triggers
a reallocation or not, and so both cases are examined.
</p>
<p>
Finally, even for a container as simple as <code>std::vector</code>, there
exist implementation differences as vendors seek to offer their customers the
best possible tradeoffs. Currently there are three shipping C++11
implementations of <code>std::vector</code>: LLVM's libc++, gcc's libstdc++,
and Microsoft's Visual Studio. The latest shipping version of each of these
three implementations are tested. As it turns out, one gets different results
for each implementation.
</p>
<p>
For each experiment, a <code>vector</code> of three items is constructed, and
then a new <code>X</code> is inserted at the front of the <code>vector</code>.
Only during the insertion are the member function calls measured.
</p>
<a name="Results"></a><h2>The Results</h2>
<a name="lvalue_no_realloc"></a><h3>lvalue with no reallocation</h3>
<p>
This test inserts an lvalue <code>X</code> into the front of a three-element
<code>vector</code>, ensuring that the insertion will not cause a
reallocation. Here is the complete code which was used to run this experiment:
</p>
<blockquote><pre>
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--insert lvalue no reallocation--\n";
X::sp = {};
v.insert(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace lvalue no reallocation--\n";
X::sp = {};
v.emplace(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
}
</pre></blockquote>
<p>
The code <code>X::sp = {};</code> zeros out all of the counters, and then
those counts are output to <code>cout</code> directly after the measured
call (to <code>insert</code> or <code>emplace</code>). Thus the calls
into <code>X</code> used to build the three-element <code>vector</code> are
not measured.
</p>
<img src="lv.jpg">
<p>
The stacked-bar chart gives the number of times each member is called for
each run of the experiment. The key goes as follows:
</p>
<ul>
<li>c (blue) is the <code>X(int, int*)</code> constructor</li>
<li>dt (green) is the destructor</li>
<li>cc (yellow) is the copy constructor</li>
<li>ca (orange) is the copy assignment operator</li>
<li>mc (red) is the move constructor</li>
<li>ma (purple) is the move assignment operator</li>
</ul>
<p>
For example libc++'s <code>insert</code> function called the copy assignment
operator once, the move constructor once, and the move assignment operator
twice.
</p>
<p>
It is hereby noted that these charts incorrectly imply that all of these
calls have the same expense. But in many cases a copy construction may be
significantly more expensive than a move construction. In other cases the
two will be identical. These results purposefully ignore such details.
Instead the data is simply presented and it is up to the reader of this paper
to give relative importance to each call.
</p>
<p>
In this particular experiment, we have very mixed results.
</p>
<ul>
<li>On libc++ and vs2013, <code>insert</code> is arguably better than
<code>emplace</code>.</li>
<li>On libstdc++, <code>emplace</code> is arguably better than
<code>insert</code>.</li>
</ul>
<p>
It is possible that a future version of libstdc++ might reduce the number
of constructions (and thus destructions) it does. libc++ shows this is
possible, and demonstrates what I believe is a minimum number of calls.
</p>
<p>
The libc++ and libstdc++ <code>emplace</code> functions have identical test
results.
</p>
<a name="xvalue_no_realloc"></a><h3>xvalue with no reallocation</h3>
<p>
This experiment differs from the previous experiment in these two lines
of code:
</p>
<blockquote><pre>
v.insert(v.begin(), std::move(x));
v.emplace(v.begin(), std::move(x));
</pre></blockquote>
<img src="xv.jpg">
<p>
When inserting an xvalue, only libc++ demonstrates an advantage of
<code>insert</code> over <code>emplace</code>. The other two platforms
have identical behavior for <code>insert</code> and <code>emplace</code>.
Furthermore the libc++ <code>emplace</code> tests identically with the
libstdc++ <code>emplace</code>.
</p>
<a name="prvalue_no_realloc"></a><h3>prvalue with no reallocation</h3>
<p>
To test prvalues, the two lines of code are modified like so:
</p>
<blockquote><pre>
v.insert(v.begin(), X{0,0});
v.emplace(v.begin(), X{0,0});
</pre></blockquote>
<img src="prv.jpg">
<p>
These results are quite similar to the xvalue results except that an extra
construction and destruction are measured on this test, which reflects the
temporary <code>X{0,0}</code> getting constructed and destructed.
</p>
<a name="lvalue_realloc"></a><h3>lvalue with reallocation</h3>
<p>
This test is identical to the <a href="#lvalue_no_realloc">lvalue with no
reallocation</a> test except that <code>v.reserve(4)</code> is changed to
<code>v.reserve(3)</code>.
</p>
<img src="lv_realloc.jpg">
<p>
When inserting an xvalue in the reallocating case, only vs2013 demonstrates
an advantage of <code>insert</code> over <code>emplace</code>. The other two
platforms have identical behavior for <code>insert</code> and
<code>emplace</code>. Furthermore the libc++ and libstdc++ platforms test
identically.
</p>
<a name="xvalue_realloc"></a><h3>xvalue with reallocation</h3>
<p>
This test is identical to the <a href="#xvalue_no_realloc">xvalue with no
reallocation</a> test except that <code>v.reserve(4)</code> is changed to
<code>v.reserve(3)</code>.
</p>
<img src="xv_realloc.jpg">
<p>
For this test each of the three platforms show no difference between
<code>insert</code> and <code>emplace</code> for this test. Furthermore
libc++ and libstdc++ test identically.
</p>
<a name="prvalue_realloc"></a><h3>prvalue with reallocation</h3>
<p>
This test is identical to the <a href="#prvalue_no_realloc">prvalue with no
reallocation</a> test except that <code>v.reserve(4)</code> is changed to
<code>v.reserve(3)</code>.
</p>
<img src="prv_realloc.jpg">
<p>
For this test each of the three platforms show no difference between
<code>insert</code> and <code>emplace</code> for this test. Furthermore
libc++ and libstdc++ test identically.
</p>
<a name="Summary"></a><h2>Summary</h2>
<p>
I was hoping for a nice clean message, however these results are more
complicated than that. On libc++ and vs2013, one can say that
<code>insert</code> is always better than, or as good as
<code>emplace</code>. But on libstdc++ you can say exactly the opposite. On
libstdc++ only in one out of the six cases is <code>emplace</code> superior
to <code>insert</code>. On vs2013, two out of the six cases have
<code>insert</code> superior to <code>emplace</code>. And on libc++ three
out of the six cases measure <code>insert</code> superior to
<code>emplace</code>. It also appears that libc++ represents the theoretical
minimum for the number of calls to <code>X</code>'s member functions under
<code>insert</code> and <code>emplace</code>. In no test did libc++ make more
calls than did libstdc++ or vs2013. Therefore one might speculate that the
results of some of the tests for libstdc++ and vs2013 have the potential to
drop in the future.
</p>
<p>
During the course of writing this paper I also collected data for
<code>vector::push_back</code> and <code>emplace_back</code>. The results
for these two operations were identical, not only between
<code>push_back</code> and <code>emplace_back</code> but among all three
platforms.
</p>
<a name="Appendix"></a><h2>Appendix: The test code</h2>
<p>
Below is the complete test code used. On Visual Studio the <code>neoxcept</code>
keyword was removed.
</p>
<blockquote><pre>
#include <iostream>
#include <vector>
class X
{
int i_;
int* p_;
public:
struct special
{
unsigned c;
unsigned dt;
unsigned cc;
unsigned ca;
unsigned mc;
unsigned ma;
};
static special sp;
X(int i, int* p)
: i_(i)
, p_(p)
{
// std::cout << "X(int i, int* p)\n";
sp.c++;
}
~X()
{
// std::cout << "~X()\n";
sp.dt++;
}
X(const X& x)
: i_(x.i_)
, p_(x.p_)
{
// std::cout << "X(const X& x)\n";
sp.cc++;
}
X& operator=(const X& x)
{
i_ = x.i_;
p_ = x.p_;
// std::cout << "X& operator=(const X& x)\n";
sp.ca++;
return *this;
}
X(X&& x) noexcept
: i_(x.i_)
, p_(x.p_)
{
// std::cout << "X(X&& x)\n";
sp.mc++;
}
X& operator=(X&& x) noexcept
{
i_ = x.i_;
p_ = x.p_;
// std::cout << "X& operator=(X&& x)\n";
sp.ma++;
return *this;
}
};
std::ostream&
operator<<(std::ostream& os, X::special const& sp)
{
os << sp.c << '\n';
os << sp.dt << '\n';
os << sp.cc << '\n';
os << sp.ca << '\n';
os << sp.mc << '\n';
os << sp.ma << '\n';
return os;
}
X::special X::sp{};
int
main()
{
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--insert lvalue no reallocation--\n";
X::sp = {};
v.insert(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace lvalue no reallocation--\n";
X::sp = {};
v.emplace(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--insert xvalue no reallocation--\n";
X::sp = {};
v.insert(v.begin(), std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace xvalue no reallocation--\n";
X::sp = {};
v.emplace(v.begin(), std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--insert rvalue no reallocation--\n";
X::sp = {};
v.insert(v.begin(), X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace rvalue no reallocation--\n";
X::sp = {};
v.emplace(v.begin(), X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--insert lvalue reallocation--\n";
X::sp = {};
v.insert(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace lvalue reallocation--\n";
X::sp = {};
v.emplace(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--insert xvalue reallocation--\n";
X::sp = {};
v.insert(v.begin(), std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace xvalue reallocation--\n";
X::sp = {};
v.emplace(v.begin(), std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--insert rvalue reallocation--\n";
X::sp = {};
v.insert(v.begin(), X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace rvalue reallocation--\n";
X::sp = {};
v.emplace(v.begin(), X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--push_back lvalue no reallocation--\n";
X::sp = {};
v.push_back(x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace_back lvalue no reallocation--\n";
X::sp = {};
v.emplace_back(x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--push_back xvalue no reallocation--\n";
X::sp = {};
v.push_back(std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace_back xvalue no reallocation--\n";
X::sp = {};
v.emplace_back(std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--push_back rvalue no reallocation--\n";
X::sp = {};
v.push_back(X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace_back rvalue no reallocation--\n";
X::sp = {};
v.emplace_back(X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--push_back lvalue reallocation--\n";
X::sp = {};
v.push_back(x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace_back lvalue reallocation--\n";
X::sp = {};
v.emplace_back(x);
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--push_back xvalue reallocation--\n";
X::sp = {};
v.push_back(std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x{0,0};
std::cout << "--emplace_back xvalue reallocation--\n";
X::sp = {};
v.emplace_back(std::move(x));
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--push_back rvalue reallocation--\n";
X::sp = {};
v.push_back(X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
{
std::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace_back rvalue reallocation--\n";
X::sp = {};
v.emplace_back(X{0,0});
std::cout << X::sp;
std::cout << "----\n";
}
}
</pre></blockquote>
<a name="Acknowledgments"></a><h2>Acknowledgments</h2>
<p>
I would like to thank
<a href="http://stackoverflow.com/users/15416/msalters">MSalters</a>,
<a href="http://stackoverflow.com/users/124797/jagannath">Jagannath</a>
and
<a href="http://stackoverflow.com/users/636019/ildjarn">ildjarn</a>
for helping me gather data on Visual Studio.
</p>
</body>
</html>