hachyderm.io is one of the many independent Mastodon servers you can use to participate in the fediverse.
Hachyderm is a safe space, LGBTQIA+ and BLM, primarily comprised of tech industry professionals world wide. Note that many non-user account types have restrictions - please see our About page.

Administered by:

Server stats:

9.4K
active users

Šimon Tóth

When using std::swap with user-defined types, we need to be careful with the consequences of Argument Dependent Lookup.

To properly call std::swap, we have to pull the default implementation into the local scope before making an unqualified call.

Alternatively, with C++20, a qualified call to std::ranges::swap will always do the correct thing.

Compiler Explorer link: compiler-explorer.com/z/4nY1sK

@simontoth Is there a neat trick to implement a custom swap function for my class in terms of a memberwise swap? Basically what "=default" does for operator==?

@asperamanca Sadly no, but if you only have swappable members, the default three-move swap should be generally optimal.

@simontoth I was confused recently when I found that std::ranges::swap is not std::ranges::swap_ranges when my code didn't compile.
Kind of weird that it is placed in a 'ranges' namespace.