I call the std::pair<vector<int>, int> constructor two ways:
- passing in an initializer list
- passing in an explicit vector (r-value).
For some reason, the initializer list version makes a copy (and destroys one).
Here is my minimal code sample:
auto dummy() {
return pair<vector<int>, int>{ {1,2,3,4,5}, 1};
}
auto dummy1() {
return pair<vector<int>, int>{ vector{1,2,3,4,5}, 1};
}
auto dummy2() {
return optional<vector<int> > { {1,2,3,4,5} };
}
After checking the compiler explorer, I found that the initializer list dummy() version calls operator new twice, and delete once. This happens with neither the explicit construction version dummy1() nor a similar std::optional constructor in dummy2(). I wouldn't expect this behavior. Does anyone know why? I checked with clang as well.