Reputation: 1988
When compiling using gcc 14.2 (or Clang), std::vector::shrink_to_fit()
has no effect when exceptions are disabled. This can be demonstrated through the following code (Live on Compiler Explorer):
#include <vector>
#include <iostream>
int main()
{
#ifdef __cpp_exceptions
std::cout << "with exceptions" << '\n';
#endif
auto v = std::vector<int>(100);
v.resize(42);
v.shrink_to_fit();
std::cout << v.capacity() << '\n'; // prints 100 when exceptions are disabled, 42 otherwise
{
auto v2 = v;
v.swap(v2);
}
std::cout << v.capacity() << '\n'; // prints 42
return 0;
}
Although shrink_to_fit()
is non-binding, I would not have expected disabling exceptions to cause this difference in behavior. What is the rationale for this library implementation choice?
Bonus question: Does -fno-exceptions
cause other subtle/unexpected libstdc++
differences that can potentially impact program performance?
Upvotes: 2
Views: 136