Title
`std::swap` should specially handle `std::locale`
Status
new
Section
[locale.syn]
Submitter
Jiang An

Created on 2026-07-31.00:00:00 last changed 4 days ago

Messages

Date: 2026-08-01.12:49:31

Proposed resolution:

This wording is relative to N5054.

  1. Modify [locale.syn] as indicated:

    namespace std {
      // [locale], locale
      class locale;
      
      void swap(locale& lhs, locale& rhs) noexcept;
      
      template<class Facet> const Facet& use_facet(const locale&);
      template<class Facet> bool has_facet(const locale&) noexcept;
      […]
    }
    
  2. Add a new sub-clause [locale.nonmembers] immediately after the existing [locale.statics] as indicated:

    28.3.3.1.? Non-member functions [locale.nonmembers]

    void swap(locale& lhs, locale& rhs) noexcept;
    

    -?- Effects: Exchanges the values of `lhs` and `rhs`.

Date: 2026-07-31.00:00:00

Currently, `std::locale` lacks move functions, and `std::swap` uses copy constructor and copy assignment operator to swap `locale` objects according to the default mechanism.

The lack of move functions is possibly intended. Because in some implementations (at least for libc++ and libstdc++), some precondition-free functions are assuming that a valid `locale` object is always resource-holding, and it's possibly infeasible to establish a valid moved-from state for `locale`. However, it's pessimized to use copy functions to swap `locale` objects since this requires increments and decrements of reference counts in mainstream implementations. An optimized strategy might be just swapping the internal pointers, which doesn't need to touch reference counts.

It seems possible to optimize `std::swap` for `locale` without affecting overload resolution. So, this seems able to be done without tweaking the standard wording. However, some implementors think it's simpler to just add a new overload (see llvm/llvm-project#209760). This approach needs to tweak the standard wording because the effects of the new overload in overload resolution are observable.

History
Date User Action Args
2026-08-01 12:49:31adminsetmessages: + msg16549
2026-07-31 00:00:00admincreate