Will std::swap still be defined by including algorithm in C++0x?
The swap
function template was moved from <algorithm>
to <utility>
in C++0x. Does the former include the latter in C++0x? Or do they both include a common header the defines swap
?
In other words, is the following code guaranteed to compile in C++0x?
#include <algorithm> // will this pu开发者_运维问答ll in std::swap?
// ...
using std::swap;
swap(a, b);
The FDIS (n3290), in Annex C, "Compatibility", 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>.
So no, it's not guaranteed to compile, this is intentionally a breaking change. Whether individual implementations will actually break C++03 code is another matter. As you point out it's easy enough for them not to, by defining swap
via either header. But there's a choice between making it easier to port C++03 code to C++0x, vs. helping people write strictly conforming C++0x.
精彩评论