admin管理员组文章数量:1123940
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?
本文标签: cWhy does stdvectorshrinktofit() have no effect when exceptions are disabledStack Overflow
版权声明:本文标题:c++ - Why does std::vector::shrink_to_fit() have no effect when exceptions are disabled? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736608761a1945391.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论