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 pull in std::swap?
// ...
using std::swap;
swap(a, b);
swap() in C++ The function std::swap() is a built-in function in the C++ Standard Template Library (STL) which swaps the value of two variables.
It is not atomic. Atomic operations are not cheap and 99% of the time you do not need the atomicity. There are (IIRC) some other means to get atomic operations but std::swap() is not one of them.
std::swap() is a built-in function in C++'s Standard Template Library. The function takes two values as input and swaps them.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With