fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class X
  5. {
  6. int i_;
  7. int* p_;
  8.  
  9. public:
  10. struct special
  11. {
  12. unsigned c;
  13. unsigned dt;
  14. unsigned cc;
  15. unsigned ca;
  16. unsigned mc;
  17. unsigned ma;
  18. };
  19. static special sp;
  20.  
  21. X(int i, int* p)
  22. : i_(i)
  23. , p_(p)
  24. {
  25. // std::cout << "X(int i, int* p)\n";
  26. sp.c++;
  27. }
  28.  
  29. ~X()
  30. {
  31. // std::cout << "~X()\n";
  32. sp.dt++;
  33. }
  34.  
  35. X(const X& x)
  36. : i_(x.i_)
  37. , p_(x.p_)
  38. {
  39. // std::cout << "X(const X& x)\n";
  40. sp.cc++;
  41. }
  42.  
  43. X& operator=(const X& x)
  44. {
  45.  
  46. i_ = x.i_;
  47. p_ = x.p_;
  48. // std::cout << "X& operator=(const X& x)\n";
  49. sp.ca++;
  50. return *this;
  51. }
  52.  
  53. X(X&& x) noexcept
  54. : i_(x.i_)
  55. , p_(x.p_)
  56. {
  57. // std::cout << "X(X&& x)\n";
  58. sp.mc++;
  59. }
  60.  
  61. X& operator=(X&& x) noexcept
  62. {
  63.  
  64. i_ = x.i_;
  65. p_ = x.p_;
  66. // std::cout << "X& operator=(X&& x)\n";
  67. sp.ma++;
  68. return *this;
  69. }
  70.  
  71. };
  72.  
  73. std::ostream&
  74. operator<<(std::ostream& os, X::special const& sp)
  75. {
  76. os << sp.c << '\n';
  77. os << sp.dt << '\n';
  78. os << sp.cc << '\n';
  79. os << sp.ca << '\n';
  80. os << sp.mc << '\n';
  81. os << sp.ma << '\n';
  82. return os;
  83. }
  84.  
  85. X::special X::sp{};
  86.  
  87. int
  88. main()
  89. {
  90. {
  91. std::vector<X> v;
  92. v.reserve(4);
  93. v.push_back(X(0,0));
  94. v.push_back(X(0,0));
  95. v.push_back(X(0,0));
  96. X x{0,0};
  97. std::cout << "--insert lvalue no reallocation--\n";
  98. X::sp = {};
  99. v.insert(v.begin(), x);
  100. std::cout << X::sp;
  101. std::cout << "----\n";
  102. }
  103. {
  104. std::vector<X> v;
  105. v.reserve(4);
  106. v.push_back(X(0,0));
  107. v.push_back(X(0,0));
  108. v.push_back(X(0,0));
  109. X x{0,0};
  110. std::cout << "--emplace lvalue no reallocation--\n";
  111. X::sp = {};
  112. v.emplace(v.begin(), x);
  113. std::cout << X::sp;
  114. std::cout << "----\n";
  115. }
  116. {
  117. std::vector<X> v;
  118. v.reserve(4);
  119. v.push_back(X(0,0));
  120. v.push_back(X(0,0));
  121. v.push_back(X(0,0));
  122. X x{0,0};
  123. std::cout << "--insert xvalue no reallocation--\n";
  124. X::sp = {};
  125. v.insert(v.begin(), std::move(x));
  126. std::cout << X::sp;
  127. std::cout << "----\n";
  128. }
  129. {
  130. std::vector<X> v;
  131. v.reserve(4);
  132. v.push_back(X(0,0));
  133. v.push_back(X(0,0));
  134. v.push_back(X(0,0));
  135. X x{0,0};
  136. std::cout << "--emplace xvalue no reallocation--\n";
  137. X::sp = {};
  138. v.emplace(v.begin(), std::move(x));
  139. std::cout << X::sp;
  140. std::cout << "----\n";
  141. }
  142. {
  143. std::vector<X> v;
  144. v.reserve(4);
  145. v.push_back(X(0,0));
  146. v.push_back(X(0,0));
  147. v.push_back(X(0,0));
  148. std::cout << "--insert rvalue no reallocation--\n";
  149. X::sp = {};
  150. v.insert(v.begin(), X{0,0});
  151. std::cout << X::sp;
  152. std::cout << "----\n";
  153. }
  154. {
  155. std::vector<X> v;
  156. v.reserve(4);
  157. v.push_back(X(0,0));
  158. v.push_back(X(0,0));
  159. v.push_back(X(0,0));
  160. std::cout << "--emplace rvalue no reallocation--\n";
  161. X::sp = {};
  162. v.emplace(v.begin(), X{0,0});
  163. std::cout << X::sp;
  164. std::cout << "----\n";
  165. }
  166. {
  167. std::vector<X> v;
  168. v.reserve(3);
  169. v.push_back(X(0,0));
  170. v.push_back(X(0,0));
  171. v.push_back(X(0,0));
  172. X x{0,0};
  173. std::cout << "--insert lvalue reallocation--\n";
  174. X::sp = {};
  175. v.insert(v.begin(), x);
  176. std::cout << X::sp;
  177. std::cout << "----\n";
  178. }
  179. {
  180. std::vector<X> v;
  181. v.reserve(3);
  182. v.push_back(X(0,0));
  183. v.push_back(X(0,0));
  184. v.push_back(X(0,0));
  185. X x{0,0};
  186. std::cout << "--emplace lvalue reallocation--\n";
  187. X::sp = {};
  188. v.emplace(v.begin(), x);
  189. std::cout << X::sp;
  190. std::cout << "----\n";
  191. }
  192. {
  193. std::vector<X> v;
  194. v.reserve(3);
  195. v.push_back(X(0,0));
  196. v.push_back(X(0,0));
  197. v.push_back(X(0,0));
  198. X x{0,0};
  199. std::cout << "--insert xvalue reallocation--\n";
  200. X::sp = {};
  201. v.insert(v.begin(), std::move(x));
  202. std::cout << X::sp;
  203. std::cout << "----\n";
  204. }
  205. {
  206. std::vector<X> v;
  207. v.reserve(3);
  208. v.push_back(X(0,0));
  209. v.push_back(X(0,0));
  210. v.push_back(X(0,0));
  211. X x{0,0};
  212. std::cout << "--emplace xvalue reallocation--\n";
  213. X::sp = {};
  214. v.emplace(v.begin(), std::move(x));
  215. std::cout << X::sp;
  216. std::cout << "----\n";
  217. }
  218. {
  219. std::vector<X> v;
  220. v.reserve(3);
  221. v.push_back(X(0,0));
  222. v.push_back(X(0,0));
  223. v.push_back(X(0,0));
  224. std::cout << "--insert rvalue reallocation--\n";
  225. X::sp = {};
  226. v.insert(v.begin(), X{0,0});
  227. std::cout << X::sp;
  228. std::cout << "----\n";
  229. }
  230. {
  231. std::vector<X> v;
  232. v.reserve(3);
  233. v.push_back(X(0,0));
  234. v.push_back(X(0,0));
  235. v.push_back(X(0,0));
  236. std::cout << "--emplace rvalue reallocation--\n";
  237. X::sp = {};
  238. v.emplace(v.begin(), X{0,0});
  239. std::cout << X::sp;
  240. std::cout << "----\n";
  241. }
  242.  
  243. {
  244. std::vector<X> v;
  245. v.reserve(4);
  246. v.push_back(X(0,0));
  247. v.push_back(X(0,0));
  248. v.push_back(X(0,0));
  249. X x{0,0};
  250. std::cout << "--push_back lvalue no reallocation--\n";
  251. X::sp = {};
  252. v.push_back(x);
  253. std::cout << X::sp;
  254. std::cout << "----\n";
  255. }
  256. {
  257. std::vector<X> v;
  258. v.reserve(4);
  259. v.push_back(X(0,0));
  260. v.push_back(X(0,0));
  261. v.push_back(X(0,0));
  262. X x{0,0};
  263. std::cout << "--emplace_back lvalue no reallocation--\n";
  264. X::sp = {};
  265. v.emplace_back(x);
  266. std::cout << X::sp;
  267. std::cout << "----\n";
  268. }
  269. {
  270. std::vector<X> v;
  271. v.reserve(4);
  272. v.push_back(X(0,0));
  273. v.push_back(X(0,0));
  274. v.push_back(X(0,0));
  275. X x{0,0};
  276. std::cout << "--push_back xvalue no reallocation--\n";
  277. X::sp = {};
  278. v.push_back(std::move(x));
  279. std::cout << X::sp;
  280. std::cout << "----\n";
  281. }
  282. {
  283. std::vector<X> v;
  284. v.reserve(4);
  285. v.push_back(X(0,0));
  286. v.push_back(X(0,0));
  287. v.push_back(X(0,0));
  288. X x{0,0};
  289. std::cout << "--emplace_back xvalue no reallocation--\n";
  290. X::sp = {};
  291. v.emplace_back(std::move(x));
  292. std::cout << X::sp;
  293. std::cout << "----\n";
  294. }
  295. {
  296. std::vector<X> v;
  297. v.reserve(4);
  298. v.push_back(X(0,0));
  299. v.push_back(X(0,0));
  300. v.push_back(X(0,0));
  301. std::cout << "--push_back rvalue no reallocation--\n";
  302. X::sp = {};
  303. v.push_back(X{0,0});
  304. std::cout << X::sp;
  305. std::cout << "----\n";
  306. }
  307. {
  308. std::vector<X> v;
  309. v.reserve(4);
  310. v.push_back(X(0,0));
  311. v.push_back(X(0,0));
  312. v.push_back(X(0,0));
  313. std::cout << "--emplace_back rvalue no reallocation--\n";
  314. X::sp = {};
  315. v.emplace_back(X{0,0});
  316. std::cout << X::sp;
  317. std::cout << "----\n";
  318. }
  319. {
  320. std::vector<X> v;
  321. v.reserve(3);
  322. v.push_back(X(0,0));
  323. v.push_back(X(0,0));
  324. v.push_back(X(0,0));
  325. X x{0,0};
  326. std::cout << "--push_back lvalue reallocation--\n";
  327. X::sp = {};
  328. v.push_back(x);
  329. std::cout << X::sp;
  330. std::cout << "----\n";
  331. }
  332. {
  333. std::vector<X> v;
  334. v.reserve(3);
  335. v.push_back(X(0,0));
  336. v.push_back(X(0,0));
  337. v.push_back(X(0,0));
  338. X x{0,0};
  339. std::cout << "--emplace_back lvalue reallocation--\n";
  340. X::sp = {};
  341. v.emplace_back(x);
  342. std::cout << X::sp;
  343. std::cout << "----\n";
  344. }
  345. {
  346. std::vector<X> v;
  347. v.reserve(3);
  348. v.push_back(X(0,0));
  349. v.push_back(X(0,0));
  350. v.push_back(X(0,0));
  351. X x{0,0};
  352. std::cout << "--push_back xvalue reallocation--\n";
  353. X::sp = {};
  354. v.push_back(std::move(x));
  355. std::cout << X::sp;
  356. std::cout << "----\n";
  357. }
  358. {
  359. std::vector<X> v;
  360. v.reserve(3);
  361. v.push_back(X(0,0));
  362. v.push_back(X(0,0));
  363. v.push_back(X(0,0));
  364. X x{0,0};
  365. std::cout << "--emplace_back xvalue reallocation--\n";
  366. X::sp = {};
  367. v.emplace_back(std::move(x));
  368. std::cout << X::sp;
  369. std::cout << "----\n";
  370. }
  371. {
  372. std::vector<X> v;
  373. v.reserve(3);
  374. v.push_back(X(0,0));
  375. v.push_back(X(0,0));
  376. v.push_back(X(0,0));
  377. std::cout << "--push_back rvalue reallocation--\n";
  378. X::sp = {};
  379. v.push_back(X{0,0});
  380. std::cout << X::sp;
  381. std::cout << "----\n";
  382. }
  383. {
  384. std::vector<X> v;
  385. v.reserve(3);
  386. v.push_back(X(0,0));
  387. v.push_back(X(0,0));
  388. v.push_back(X(0,0));
  389. std::cout << "--emplace_back rvalue reallocation--\n";
  390. X::sp = {};
  391. v.emplace_back(X{0,0});
  392. std::cout << X::sp;
  393. std::cout << "----\n";
  394. }
  395. }
  396.  
Success #stdin #stdout 0s 3444KB
stdin
Standard input is empty
stdout
--insert lvalue no reallocation--
0
2
1
0
2
3
----
--emplace lvalue no reallocation--
0
1
1
0
1
3
----
--insert xvalue no reallocation--
0
1
0
0
2
3
----
--emplace xvalue no reallocation--
0
1
0
0
2
3
----
--insert rvalue no reallocation--
1
2
0
0
2
3
----
--emplace rvalue no reallocation--
1
2
0
0
2
3
----
--insert lvalue reallocation--
0
3
1
0
3
0
----
--emplace lvalue reallocation--
0
3
1
0
3
0
----
--insert xvalue reallocation--
0
3
0
0
4
0
----
--emplace xvalue reallocation--
0
3
0
0
4
0
----
--insert rvalue reallocation--
1
4
0
0
4
0
----
--emplace rvalue reallocation--
1
4
0
0
4
0
----
--push_back lvalue no reallocation--
0
0
1
0
0
0
----
--emplace_back lvalue no reallocation--
0
0
1
0
0
0
----
--push_back xvalue no reallocation--
0
0
0
0
1
0
----
--emplace_back xvalue no reallocation--
0
0
0
0
1
0
----
--push_back rvalue no reallocation--
1
1
0
0
1
0
----
--emplace_back rvalue no reallocation--
1
1
0
0
1
0
----
--push_back lvalue reallocation--
0
3
1
0
3
0
----
--emplace_back lvalue reallocation--
0
3
1
0
3
0
----
--push_back xvalue reallocation--
0
3
0
0
4
0
----
--emplace_back xvalue reallocation--
0
3
0
0
4
0
----
--push_back rvalue reallocation--
1
4
0
0
4
0
----
--emplace_back rvalue reallocation--
1
4
0
0
4
0
----