Why was std::swap moved to <utility>?

流过昼夜 提交于 2020-04-08 08:47:12

问题


Why has std::swap been moved to the <utility> header for C++11?

N3290 C.2.7 says:

17.6.3.2

Effect on original feature: Function swap moved to a different header

Rationale: Remove dependency on <algorithm>for swap.

Effect on original feature: Valid C++ 2003 code that has been compiled expecting swap to be in <algorithm> may have to instead include <utility>.

I can't understand the part in bold. What kind of dependency is being talked about and why?


回答1:


The committee wanted to allow you to use swap() without introducing a compile-time dependency on the large and more complex <algorithm> header file. Because swap() is so widely used, it makes sense to let you pull in its definition with as little additional baggage as possible; this will generally lead to faster compile times for files that don't otherwise need <algorithm>. Its new home allows it to be used without introducing unneeded overhead.



来源:https://stackoverflow.com/questions/6086072/why-was-stdswap-moved-to-utility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!