Title
Variants cannot properly support allocators
Status
c++17
Section
[variant.variant]
Submitter
United States

Created on 2017-02-03.00:00:00 last changed 89 months ago

Messages

Date: 2017-07-15.22:46:09

Proposed resolution:

This wording is relative to N4659.

  1. Change [variant.syn], header <variant> synopsis, as follows:

    […]
    // [variant.traits], allocator-related traits
    template <class T, class Alloc> struct uses_allocator;
    template <class... Types, class Alloc> struct uses_allocator<variant<Types...>, Alloc>;
    
  2. Change [variant.variant], class template variant synopsis, as follows:

    […]
    // allocator-extended constructors
    template <class Alloc>
      variant(allocator_arg_t, const Alloc&);
    template <class Alloc>
      variant(allocator_arg_t, const Alloc&, const variant&);
    template <class Alloc>
      variant(allocator_arg_t, const Alloc&, variant&&);
    template <class Alloc, class T>
      variant(allocator_arg_t, const Alloc&, T&&);
    template <class Alloc, class T, class... Args>
      variant(allocator_arg_t, const Alloc&, in_place_type_t<T>, Args&&...);
    template <class Alloc, class T, class U, class... Args>
      variant(allocator_arg_t, const Alloc&, in_place_type_t<T>,
              initializer_list<U>, Args&&...);
    template <class Alloc, size_t I, class... Args>
      variant(allocator_arg_t, const Alloc&, in_place_index_t<I>, Args&&...);
    template <class Alloc, size_t I, class U, class... Args>
      variant(allocator_arg_t, const Alloc&, in_place_index_t<I>,
              initializer_list<U>, Args&&...);
    
  3. Modify [variant.ctor] as indicated:

    // allocator-extended constructors
    template <class Alloc>
      variant(allocator_arg_t, const Alloc& a);
    template <class Alloc>
      variant(allocator_arg_t, const Alloc& a, const variant& v);
    template <class Alloc>
      variant(allocator_arg_t, const Alloc& a, variant&& v);
    template <class Alloc, class T>
      variant(allocator_arg_t, const Alloc& a, T&& t);
    template <class Alloc, class T, class... Args>
      variant(allocator_arg_t, const Alloc& a, in_place_type_t<T>, Args&&... args);
    template <class Alloc, class T, class U, class... Args>
      variant(allocator_arg_t, const Alloc& a, in_place_type_t<T>,
              initializer_list<U> il, Args&&... args);
    template <class Alloc, size_t I, class... Args>
      variant(allocator_arg_t, const Alloc& a, in_place_index_t<I>, Args&&... args);
    template <class Alloc, size_t I, class U, class... Args>
      variant(allocator_arg_t, const Alloc& a, in_place_index_t<I>,
              initializer_list<U> il, Args&&... args);
    

    -34- Requires: Alloc shall meet the requirements for an Allocator ([allocator.requirements]).

    -35- Effects: Equivalent to the preceding constructors except that the contained value is constructed with uses-allocator construction ([allocator.uses.construction]).

  4. Modify [variant.traits] as indicated:

    template <class... Types, class Alloc>
    struct uses_allocator<variant<Types...>, Alloc> : true_type { };
    

    -1- Requires: Alloc shall be an Allocator ([allocator.requirements]).

    -2- [Note: Specialization of this trait informs other library components that variant can be constructed with an allocator, even though it does not have a nested allocator_type. — end note]

Date: 2017-07-15.22:46:09

[ 2017-07 Toronto Moved to Immediate ]

Date: 2017-06-29.00:00:00

[ 2017-06-29 Moved to Tentatively Ready after 7 positive votes on c++std-lib. ]

Date: 2017-02-15.00:00:00

[ 2017-02-28, Kona, Casey provides wording ]

Date: 2017-02-03.00:00:00
Addresses US 113

Variants cannot properly support allocators, as any assignment of a subsequent value throws away the allocator used at construction. This is not an easy problem to solve, so variant would be better served dropping the illusion of allocator support for now, leaving open the possibility to provide proper support once the problems are fully understood.

Proposed change: Strike the 8 allocator aware constructor overloads from the class definition, and strike 20.7.2.1 [variant.ctor] p34/35. Strike clause 20.7.12 [variant.traits]. Strike the specialization of uses_allocator for variant in the <variant> header synopsis, 20.7.1 [variant.general].

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> c++17
2017-07-16 20:47:13adminsetstatus: immediate -> wp
2017-07-15 22:46:09adminsetmessages: + msg9370
2017-07-15 22:46:09adminsetstatus: ready -> immediate
2017-06-29 16:40:08adminsetmessages: + msg9314
2017-06-29 16:40:08adminsetstatus: new -> ready
2017-02-28 05:28:49adminsetmessages: + msg9013
2017-02-28 05:28:49adminsetmessages: + msg9012
2017-02-03 00:00:00admincreate