Title
Container adapters mandate use of emplace_back but don't require it
Status
open
Section
[stack] [queue]
Submitter
Marshall Clow

Created on 2018-10-02.00:00:00 last changed 55 months ago

Messages

Date: 2020-05-09.19:18:02

Proposed resolution:

This wording is relative to N4861.

  1. Edit [stack.defn], class template stack definition, as indicated:

    template<class... Args>
      decltype(auto) emplace(Args&&... args) {
        if constexpr (requires { c.emplace_back(std::forward<Args>(args)...); }) {
          return c.emplace_back(std::forward<Args>(args)...);
        } else {
          return c.emplace(c.end(), std::forward<Args>(args)...);
        }
      }
    
  2. Edit [queue.defn], class template queue definition, as indicated:

    template<class... Args>
      decltype(auto) emplace(Args&&... args) {
        if constexpr (requires { c.emplace_back(std::forward<Args>(args)...); }) {
          return c.emplace_back(std::forward<Args>(args)...);
        } else {
          return c.emplace(c.end(), std::forward<Args>(args)...);
        }
      }
    
Date: 2020-05-15.00:00:00

[ 2020-05-09; Reflector prioritization ]

Set priority to 3 after reflector discussions.

Date: 2020-05-09.19:18:02

[ 2020-05 Casey provides new wording ]

This is the "probe for emplace_back with fallback to emplace" approach that LWG wanted to see wording for in Kona.

Date: 2020-05-04.23:20:54

[ 2019-02; Kona Wednesday night issue processing ]

Status to Open; Casey to provide updated wording, and re-vote on reflector.

Polls were: NAD - 5-1-3; "Option B" - 2-5-2 and "Probe the container" - 7-2-0

Previous resolution [SUPERSEDED]:

This wording is relative to N4762.

I have prepared two mutually exclusive options.
Option A a requirement for emplace_back to the underlying container.
Option B one replaces the calls to emplace_back with calls to emplace.

Option A

  1. Edit [stack], as indicated:

    Any sequence container supporting operations back(), push_back(), emplace_back() and pop_back() can be used to instantiate stack.

  2. Edit [queue.defn], as indicated:

    Any sequence container supporting operations front(), back(), push_back(), emplace_back() and pop_front() can be used to instantiate queue.

Option B

  1. Edit [stack.defn], class template stack definition, as indicated:

    template<class... Args>
      decltype(auto) emplace(Args&&... args)
        { return c.emplace_back(c.end(), std::forward<Args>(args)...); }
    
  2. Edit [queue.defn], class template queue definition, as indicated:

    template<class... Args>
      decltype(auto) emplace(Args&&... args)
        { return c.emplace_back(c.end(), std::forward<Args>(args)...); }
    
Date: 2018-10-06.11:12:02

[stack] p1 says:

Any sequence container supporting operations back(), push_back() and pop_back() can be used to instantiate stack.

but then in [stack.defn] we have the following code:

template<class... Args>
  decltype(auto) emplace(Args&&... args)
    { return c.emplace_back(std::forward<Args>(args)...); }

The same pattern appears in [queue].

I see two ways to resolve this:

The first is to add emplace_back() to the list of requirements for underlying containers for stack and queue

The second is to replace the calls to c.emplace_back(std::forward<Args>(args)...) with c.emplace(c.end(), std::forward<Args>(args)...). We can do this w/o messing with the list above because emplace is part of the sequence container requirements, while emplace_back is not. I checked the libc++ implementation of vector, deque, and list, and they all do the same thing for emplace(end(), ...) and emplace_back(...).

History
Date User Action Args
2020-05-09 19:18:02adminsetmessages: + msg11273
2020-05-04 23:20:54adminsetmessages: + msg11271
2019-02-21 17:23:36adminsetmessages: + msg10319
2019-02-21 17:23:36adminsetstatus: new -> open
2018-10-02 17:25:26adminsetmessages: + msg10155
2018-10-02 00:00:00admincreate