Skip to content

Explicit calls to std::swap are made on user provided template types. This does not work for C++20. #72

@melton1968

Description

@melton1968

Starting with C++20, fully specializing a function template in the std namespace is undefined behavior. This precludes implementing swap for user-defined types in the standard namespace. You can find the reasoning in this paper.

I am running into this issue while using boost::sort in a project using C++20. I have implemented a free function swap for my type in the type's namespace, but since the library code is explicitly calling std::swap, it cannot find it. If I implement the free function in std, the compiler chooses to ignore it, which is allowed under the standard.

I changed the sort code to use the "use std::swap and then call swap unqualified" idiom, and everything worked as expected. For example:

std::swap(*iter1, *iter2);

becomes,

using std::swap;
swap(*iter1, *ite2);

I can submit a PR for this change if that is appropriate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions