Skip to content

<tuple>: Should allocator_arg_t constructors of tuple specially handle pair since C++20? #5668

@frederick-vs-ja

Description

@frederick-vs-ja

WG21-P0591R4 (adopted in C++20) extended the definition of uses-allocator construction to cover pair. It seems that strict reading of standard wording requires the allocator_arg_t constructors of tuple to be changed.

However, no implementation is currently doing this. Example (Godbolt link):

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

template<class T>
class payload_ator {
    int payload{};
    
public:
    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>>>;
    std::tuple<my_pair_t> t(std::allocator_arg, payload_ator<int>{in_v});
    auto out_v = std::get<0>(t).second.get_allocator().get_payload();
    return in_v == out_v;
}

int main() {
    assert(test()); // passes only if allocator_arg_t constructors of tuple specially handle pair
}

Should we update these constructors or submit an LWG issue to avoid changing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    LWG issue neededA wording defect that should be submitted to LWG as a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions