Title
`enable_nonlocking_formatter_optimization` should be disabled for container adaptors
Status
new
Section
[queue.syn][stack.syn]
Submitter
Tomasz Kamiński

Created on 2025-10-02.00:00:00 last changed 1 week ago

Messages

Date: 2025-10-04.11:13:52

Proposed resolution:

This wording is relative to N5014.

  1. Modify [queue.syn], header <queue> synopsis, as indicated:

    […]
    // [container.adaptors.format], formatter specialization for queue
    template<class charT, class T, formattable<charT> Container>
      struct formatter<queue<T, Container>, charT>;
      
    template<class T, class Container>
      constexpr bool enable_nonlocking_formatter_optimization<queue<T, Container>> = false;
    
    // [priority.queue], class template priority_queue
    template<class T, class Container = vector<T>,
             class Compare = less<typename Container::value_type>>
      class priority_queue;
    […]
    // [container.adaptors.format], formatter specialization for priority_queue
    template<class charT, class T, formattable<charT> Container, class Compare>
      struct formatter<priority_queue<T, Container, Compare>, charT>;
      
    template<class T, class Container, class Compare>
      constexpr bool enable_nonlocking_formatter_optimization<priority_queue<T, Container, Compare>> = false;
    […]
    
  2. Modify [stack.syn], header <stack> synopsis, as indicated:

    […]
    
    // [container.adaptors.format], formatter specialization for stack
    template<class charT, class T, formattable<charT> Container>
      struct formatter<stack<T, Container>, charT>;
    
    template<class T, class Container>
      constexpr bool enable_nonlocking_formatter_optimization<stack<T, Container>> = false;
    
    […]
    
Date: 2025-10-10.13:34:43

As the standard currently defines formatters for `queue`, `prioriy_queue`, and `stack` `enable_nonlocking_formatter_optimization` is specialized to `true` for these adaptors per [format.formatter.spec] p3:

Unless specified otherwise, for each type `T` for which a formatter specialization is provided by the library, each of the headers provides the following specialization:

template<> inline constexpr bool enable_nonlocking_formatter_optimization<T> = true;

However, formatting an adaptor requires formatting of the underlying range in terms of `ranges::ref_view`, and we disable the nonlocking_optimizations for all ranges, including `ranges::ref_view`.

This problem does not occur for the `flat_set`, `flat_map` adaptors, which are also ranges, but unlike `stack` etc. they do not have a specialized formatter. They use the `formatter` specialization for ranges and we already disable the optimization for that formatter.

The proposed wording has recently been implemented in gcc's libstdc++.

History
Date User Action Args
2025-10-04 11:13:52adminsetmessages: + msg15114
2025-10-02 00:00:00admincreate