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

3884. flat_foo is missing allocator-extended copy/move constructors

Section: 24.6.9 [flat.map], 24.6.10 [flat.multimap], 24.6.11 [flat.set], 24.6.12 [flat.multiset] Status: Tentatively Ready Submitter: Arthur O'Dwyer Opened: 2023-02-06 Last modified: 2023-03-23

Priority: Not Prioritized

View other active issues in [flat.map].

View all other issues in [flat.map].

View all issues with Tentatively Ready status.

Discussion:

This issue is part of the "flat_foo" sequence, LWG 3786, 3802, 3803, 3804. flat_set, flat_multiset, flat_map, and flat_multimap all have implicitly defaulted copy and move constructors, but they lack allocator-extended versions of those constructors. This means that the following code is broken:

// https://godbolt.org/z/qezv5rTrW
#include <cassert>
#include <flat_set>
#include <memory_resource>
#include <utility>
#include <vector>

template<class T, class Comp = std::less<T>>
using pmr_flat_set = std::flat_set<T, Comp, std::pmr::vector<T>>;

int main() {
  std::pmr::vector<pmr_flat_set<int>> vs = {
    {1,2,3},
    {4,5,6},
  };
  std::pmr::vector<int> v = std::move(vs[0]).extract();
  assert(v.get_allocator().resource() == std::pmr::get_default_resource());
}

pmr_flat_set<int> advertises that it "uses_allocator" std::pmr::polymorphic_allocator<int>, but in fact it lacks the allocator-extended constructor overload set necessary to interoperate with std::pmr::vector.

[2023-03-22; Reflector poll]

Set status to Tentatively Ready after five votes in favour during reflector poll.

Proposed resolution:

This wording is relative to N4928.

  1. Modify 24.6.9.2 [flat.map.defn] as indicated:

    namespace std {
      template<class Key, class T, class Compare = less<Key>,
      class KeyContainer = vector<Key>, class MappedContainer = vector<T>>
      class flat_map {
      public:
        […]
        // 24.6.9.3 [flat.map.cons], construct/copy/destroy
        flat_map() : flat_map(key_compare()) { }
    
        template<class Allocator>
          flat_map(const flat_map&, const Allocator& a);
        template<class Allocator>
          flat_map(flat_map&&, const Allocator& a);
    
        […]
      };
      […]
    }
    
  2. Modify 24.6.9.3 [flat.map.cons] as indicated:

    [Drafting note: As a drive-by fix, a missing closing > is inserted in p11.]

    template<class Allocator>
      flat_map(const flat_map&, const Allocator& a);
    template<class Allocator>
      flat_map(flat_map&&, const Allocator& a);
    template<class Allocator>
      flat_map(const key_compare& comp, const Allocator& a);
    template<class Allocator>
      explicit flat_map(const Allocator& a);
    template<class InputIterator, class Allocator>
      flat_map(InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a);
    template<class InputIterator, class Allocator>
      flat_map(InputIterator first, InputIterator last, const Allocator& a);
    […]
    

    -11- Constraints: uses_allocator_v<key_container_type, Allocator> is true and uses_allocator_v<mapped_container_type, Allocator> is true.

    -12- Effects: Equivalent to the corresponding non-allocator constructors except that c.keys and c.values are constructed with uses-allocator construction (20.2.8.2 [allocator.uses.construction]).

  3. Modify 24.6.10.2 [flat.multimap.defn] as indicated:

    namespace std {
      template<class Key, class T, class Compare = less<Key>,
      class KeyContainer = vector<Key>, class MappedContainer = vector<T>>
      class flat_multimap {
      public:
        […]
        // 24.6.10.3 [flat.multimap.cons], construct/copy/destroy
        flat_multimap() : flat_multimap(key_compare()) { }
    
        template<class Allocator>
          flat_multimap(const flat_multimap&, const Allocator& a);
        template<class Allocator>
          flat_multimap(flat_multimap&&, const Allocator& a);
    
        […]
      };
      […]
    }
    
  4. Modify 24.6.10.3 [flat.multimap.cons] as indicated:

    template<class Allocator>
      flat_multimap(const flat_multimap&, const Allocator& a);
    template<class Allocator>
      flat_multimap(flat_multimap&&, const Allocator& a);
    template<class Allocator>
      flat_multimap(const key_compare& comp, const Allocator& a);
    […]
    

    -11- Constraints: uses_allocator_v<key_container_type, Allocator> is true and uses_allocator_v<mapped_container_type, Allocator> is true.

    -12- Effects: Equivalent to the corresponding non-allocator constructors except that c.keys and c.values are constructed with uses-allocator construction (20.2.8.2 [allocator.uses.construction]).

  5. Modify 24.6.11.2 [flat.set.defn] as indicated:

    namespace std {
      template<class Key, class Compare = less<Key>, class KeyContainer = vector<Key>>
      class flat_set {
      public:
        […]
        // 24.6.11.3 [flat.set.cons], constructors
        flat_set() : flat_set(key_compare()) { }
    
        template<class Allocator>
          flat_set(const flat_set&, const Allocator& a);
        template<class Allocator>
          flat_set(flat_set&&, const Allocator& a);
    
        […]
      };
      […]
    }
    
  6. Modify 24.6.11.3 [flat.set.cons] as indicated:

    template<class Allocator>
      flat_set(const flat_set&, const Allocator& a);
    template<class Allocator>
      flat_set(flat_set&&, const Allocator& a);
    template<class Allocator>
      flat_set(const key_compare& comp, const Allocator& a);
    […]
    

    -9- Constraints: uses_allocator_v<container_type, Allocator> is true.

    -10- Effects: Equivalent to the corresponding non-allocator constructors except that c is constructed with uses-allocator construction (20.2.8.2 [allocator.uses.construction]).

  7. Modify 24.6.12.2 [flat.multiset.defn] as indicated:

    namespace std {
      template<class Key, class Compare = less<Key>, class KeyContainer = vector<Key>>
      class flat_multiset {
      public:
        […]
        // 24.6.12.3 [flat.multiset.cons], constructors
        flat_multiset() : flat_multiset(key_compare()) { }
    
        template<class Allocator>
          flat_multiset(const flat_multiset&, const Allocator& a);
        template<class Allocator>
          flat_multiset(flat_multiset&&, const Allocator& a);
    
        […]
      };
      […]
    }
    
  8. Modify 24.6.12.3 [flat.multiset.cons] as indicated:

    template<class Allocator>
      flat_multiset(const flat_multiset&, const Allocator& a);
    template<class Allocator>
      flat_multiset(flat_multiset&&, const Allocator& a);
    template<class Allocator>
      flat_multiset(const key_compare& comp, const Allocator& a);
    […]
    

    -9- Constraints: uses_allocator_v<container_type, Allocator> is true.

    -10- Effects: Equivalent to the corresponding non-allocator constructors except that c is constructed with uses-allocator construction (20.2.8.2 [allocator.uses.construction]).