Created
September 2, 2025 07:34
-
-
Save flomnes/946cfeb4bd1dca0e467a8304d895e340 to your computer and use it in GitHub Desktop.
Using std::vector::emplace_back avoids useless constructions of temporaries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include "verbose.hpp" | |
int main() | |
{ | |
std::cout << "#1" << std::endl; | |
{ | |
std::vector<Verbose> f; | |
for (int ii = 0; ii < 2; ii++) | |
{ | |
f.push_back(Verbose(5)); | |
} | |
} | |
std::cout << "#2" << std::endl; | |
{ | |
std::vector<Verbose> f; | |
for (int ii = 0; ii < 2; ii++) | |
{ | |
f.emplace_back(5); | |
} | |
} | |
} | |
/* | |
Possible outout | |
#1 | |
Verbose::Verbose(int) this=0x7ffcbaeeb024 | |
Verbose::Verbose(Verbose&&) this=0x5635474d32c0 other=0x7ffcbaeeb024 | |
Verbose::~Verbose() this=0x7ffcbaeeb024 | |
Verbose::Verbose(int) this=0x7ffcbaeeb024 | |
Verbose::Verbose(Verbose&&) this=0x5635474d32e4 other=0x7ffcbaeeb024 | |
Verbose::Verbose(Verbose&&) this=0x5635474d32e0 other=0x5635474d32c0 | |
Verbose::~Verbose() this=0x5635474d32c0 | |
Verbose::~Verbose() this=0x7ffcbaeeb024 | |
Verbose::~Verbose() this=0x5635474d32e0 | |
Verbose::~Verbose() this=0x5635474d32e4 | |
#2 | |
Verbose::Verbose(int) this=0x5635474d32e0 | |
Verbose::Verbose(int) this=0x5635474d32c4 | |
Verbose::Verbose(Verbose&&) this=0x5635474d32c0 other=0x5635474d32e0 | |
Verbose::~Verbose() this=0x5635474d32e0 | |
Verbose::~Verbose() this=0x5635474d32c0 | |
Verbose::~Verbose() this=0x5635474d32c4 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment