Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LWG-3900 The allocator_arg_t overloads of generator::promise_type::operator new should not be constrained #5150

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions stl/inc/generator
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,18 @@ namespace _Gen_detail {
}

template <class _Alloc2, class... _Args>
requires convertible_to<const _Alloc2&, _Proto_allocator>
_NODISCARD_RAW_PTR_ALLOC static void* operator new(
const size_t _Size, allocator_arg_t, const _Alloc2& _Al, const _Args&...) {
static_assert(convertible_to<const _Alloc2&, _Proto_allocator>,
"argument convertible to the allocator type is required");
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
return _Allocate(static_cast<_Alloc>(static_cast<_Proto_allocator>(_Al)), _Size);
}

template <class _This, class _Alloc2, class... _Args>
requires convertible_to<const _Alloc2&, _Proto_allocator>
_NODISCARD_RAW_PTR_ALLOC static void* operator new(
const size_t _Size, const _This&, allocator_arg_t, const _Alloc2& _Al, const _Args&...) {
static_assert(convertible_to<const _Alloc2&, _Proto_allocator>,
"argument convertible to the allocator type is required");
return _Allocate(static_cast<_Alloc>(static_cast<_Proto_allocator>(_Al)), _Size);
}

Expand Down
10 changes: 4 additions & 6 deletions tests/std/tests/P2502R2_generator_promise/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,18 @@ void test_operator_new(typename Gen::promise_type& p, const Alloc2& alloc2 = {})
}

// Test 'operator new(size_t, allocator_arg_t, const Alloc2&, const Args&...)'
constexpr bool has_op_new2 = HasOperatorNew<Promise, size_t, allocator_arg_t, const Alloc2&, int, int>;
static_assert(has_op_new2 == (same_as<Alloc, void> || convertible_to<const Alloc2&, Alloc>) );
if constexpr (has_op_new2) {
// This operator new is unconstrained.
if constexpr (same_as<Alloc, void> || convertible_to<const Alloc2&, Alloc>) {
const size_t size = __STDCPP_DEFAULT_NEW_ALIGNMENT__;
void* const mem = p.operator new(size, allocator_arg, alloc2, 0, 0);
assert(reinterpret_cast<uintptr_t>(mem) % __STDCPP_DEFAULT_NEW_ALIGNMENT__ == 0);
p.operator delete(mem, size);
}

// Test 'operator new(size_t, const This&, allocator_arg_t, const Alloc2&, const Args&...)'
// This operator new is unconstrained.
struct S {};
constexpr bool has_op_new3 = HasOperatorNew<Promise, size_t, const S&, allocator_arg_t, const Alloc2&, int, int>;
static_assert(has_op_new3 == (same_as<Alloc, void> || convertible_to<const Alloc2&, Alloc>) );
if constexpr (has_op_new3) {
if constexpr (same_as<Alloc, void> || convertible_to<const Alloc2&, Alloc>) {
const size_t size = __STDCPP_DEFAULT_NEW_ALIGNMENT__;
const S s;
void* const mem = p.operator new(size, s, allocator_arg, alloc2, 0, 0);
Expand Down