In C++20, there are two swap
function templates: std::ranges::swap(T&&, U&&)
and std::swap(T&, T&)
.
I just wonder:
What's the difference between they two?
In C++20, there are two swap
function templates: std::ranges::swap(T&&, U&&)
and std::swap(T&, T&)
.
I just wonder:
What's the difference between they two?
In rough terms: std::ranges::swap
generalizes std::swap
.
T
and U
are the same, then it's equivalent to invoking std::swap
.See the cppreference page for details.
I must say I find this function to be somewhat confusing and baroque, but then - maybe people more experienced with the range library develop an intuition as to why it makes sense.
std::swap
has a problem. It's possible that there is a more efficient swap function that is not in the std
namespace. You should enable ADL to find and use that if available and use std::swap
otherwise. It's called "std 2-step":
using std::swap;
swap(a, b);
But std::ranges::swap
doesn't have this problem and will call the version ADL would have called:
std::ranges::swap(a, b);
Here is a demonstration.
std::swap
should be totally replaced with std::ranges::swap
? –
Markson std::ranges::swap
in all new code. –
Nonary swap
is very often overloaded for custom classes unlike much of the rest of the standard library. If everyone starts using the ranges version, we don't have to keep teaching folks to always remember to enable ADL before using swap
in particular. Additionally, ranges also include begin
, end
, etc which also previously required ADL to work with custom types at all. –
Nonary std::swap()
when they want to swap is not that different then having them incant the "magic spell" of using std::swap
. 3. If this is relevant for std::swap
, why not std::sort
, and all sorts of algorithms too? –
Chronicles std::swap
is very often overloaded while I've never seen std::sort
overloaded. This is a very common idiom if you google "std 2-step". This one for instance: what does using std::swap
inside the body of a class method implementation mean?. –
Nonary std::swap
is unique in that it's a free function template applicable to approximately every type, that wants to fiddle around in the private parts of classes. std::sort
et.al. are defined to use ADL when swapping, they don't need to do the privates-fiddling directly. –
Sift In rough terms: std::ranges::swap
generalizes std::swap
.
T
and U
are the same, then it's equivalent to invoking std::swap
.See the cppreference page for details.
I must say I find this function to be somewhat confusing and baroque, but then - maybe people more experienced with the range library develop an intuition as to why it makes sense.
Something not mentioned in the other answers -
Since std::ranges::swap
is a niebloid, it represents a single object that can be assigned to a variable or passed to a function.
template< typename T >
void do_something( T& a, T& b, auto function )
{
function( a, b );
}
do_something( v1, v2, std::ranges::swap );
will compile and work correctly.
do_something( v1, v2, std::swap );
will not compile, because std::swap
is both a template and an overloaded one at that.
© 2022 - 2024 — McMap. All rights reserved.