<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/67367>67367</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
std::vector is changed when copy-assignment in vector::insert(pos, const_value) throws
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
leni536
</td>
</tr>
</table>
<pre>
version: clang 17.0.1
flags: `-stdlib=libc++ -std=c++20 -O2`
## Observed behavior
```cpp
#include <vector>
#include <iostream>
struct S {
S() = default;
S(const S&) = default;
S(S&&) = default;
S& operator=(const S&) {
throw 42;
}
S& operator=(S&&) = default;
};
int main() {
std::vector<S> v;
v.reserve(4);
v.emplace_back();
const S s;
std::cout << v.size() << '\n';
try {
v.insert(v.begin(), s);
} catch (...) {
std::cout << "exception\n";
}
std::cout << v.size() << '\n';
}
```
This program prints:
```
1
exception
2
```
https://godbolt.org/z/4hqsPr137
## Expected behavior
https://timsong-cpp.github.io/cppwp/n4868/container.requirements.general#11.1
> if an exception is thrown by an `insert()` or `emplace()` function while inserting a single element, that function has no effects.
Therefore the size of the vector shouldn't change either.
## Notes
This is caused by moving the elements in the vector before copy assigning the value to be inserted to the moved-from element at the requested position.
https://github.com/llvm/llvm-project/blob/62f5636838ac80ebe8db9c31cdf9c5566db8f0c9/libcxx/include/vector#L1763
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVU1v4zgP_jXKhYjhSPHXIYe2mZxevLPA7H0gybStXUXySLLbzq9fSM5HM007WGxROJYoPnrIh6S596o3iDtSPJJiv-JTGKzbaTSqYOVK2PZ1N6PzyhrCHkBqbnrYVFmebUi-J_lDp3nvo4mU-dqHVitB2F4rIQl9JPQR4iZh-9OS5rD-SkmZL96nJ2WEMvgqPLoZWxA48FlZd3OmzJd_OY4XL2WknloEwp5mlME6wr7cMyrrg0N-vJrT0wc3yQDfgFSPyw4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