Is a cv-qualified pair specially handled in uses-allocator construction?
Jiang An

Created on 2022-02-16.00:00:00 last changed 2 months ago


Date: 2022-03-15.00:00:00

[ 2022-03-04; Reflector poll ]

Set priority to 2 after reflector poll.

Date: 2022-02-16.00:00:00

It seems unclear whether cv-qualified pair specializations are considered as specializations of pair in [allocator.uses.construction].

Currently MSVC STL only considered cv-unqualified pair types as such specializations, while libstdc++ accept both cv-unqualified and const-qualified pair types as such specializations. The resolution of LWG 3525 uses remove_cv_t, which possibly imply that the specialization of pair may be cv-qualified.

The difference can be observed via the following program:

#include <utility>
#include <memory>
#include <vector>
#include <cassert> 

template<class T>
class payload_ator {

  int payload{};
  payload_ator() = default;

  constexpr explicit payload_ator(int n) noexcept : payload{n} {}

  template<class U>
  constexpr explicit payload_ator(payload_ator<U> a) noexcept : payload{a.payload} {}   

  friend bool operator==(payload_ator, payload_ator) = default;

  template<class U>
  friend constexpr bool operator==(payload_ator x, payload_ator<U> y) noexcept
    return x.payload == y.payload;

  using value_type = T;

  constexpr T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }

  constexpr void deallocate(T* p, std::size_t n) { return std::allocator<T>{}.deallocate(p, n); }   

  constexpr int get_payload() const noexcept { return payload; }

bool test()
  constexpr int in_v = 42;
  using my_pair_t = std::pair<int, std::vector<int, payload_ator<int>>>;
  auto out_v = std::make_obj_using_allocator<const my_pair_t>(payload_ator<int>{in_v}).second.get_allocator().get_payload();
  return in_v == out_v;

int main()
  assert(test()); // passes only if a const-qualified pair specialization is considered as a pair specialization
Date User Action Args
2022-03-04 14:33:52adminsetmessages: + msg12400
2022-02-16 00:00:00admincreate