This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

4439. std::optional<T&>::swap possibly selects ADL-found swap

Section: 22.5.4.4 [optional.ref.swap] Status: New Submitter: Jiang An Opened: 2025-10-31 Last modified: 2025-10-31

Priority: Not Prioritized

View all issues with New status.

Discussion:

Currently, 22.5.4.4 [optional.ref.swap] p1 specifies an "unqualified" swap call, which possibly selects an ADL-found swap function due to 16.4.2.2 [contents] and 16.4.4.3 [swappable.requirements].

It's unlike to be intentional to call ADL-found swap on pointers (given ranges::swap doesn't), and the unconditional noexcept also suggests that user-provided swap functions shouldn't interfere with optional<T&>::swap.

Proposed resolution:

This wording is relative to N5014.

  1. Modify 22.5.4.4 [optional.ref.swap] as indicated:

    constexpr void swap(optional& rhs) noexcept;
    

    -1- Effects: Equivalent to: std::swap(val, rhs.val).