Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 pull in std::swap?

// ...

using std::swap;
swap(a, b);
like image 405
fredoverflow Avatar asked Aug 11 '11 10:08

fredoverflow


People also ask

Is there any swap function in C++?

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.

Is STD swap Atomic?

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.

What is STD swap?

std::swap() is a built-in function in C++'s Standard Template Library. The function takes two values as input and swaps them.


1 Answers

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.

like image 149
Steve Jessop Avatar answered Oct 05 '22 11:10

Steve Jessop