//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // template // struct allocator_traits // { // template // static constexpr void construct(allocator_type& a, Ptr p, Args&&... args); // ... // }; #include #include #include #include #include "test_macros.h" #include "incomplete_type_helper.h" template struct A { typedef T value_type; }; template struct B { typedef T value_type; TEST_CONSTEXPR_CXX20 B(int& count) : count_(count) {} #if TEST_STD_VER >= 11 template TEST_CONSTEXPR_CXX20 void construct(U* p, Args&& ...args) { ++count_; #if TEST_STD_VER > 17 std::construct_at(p, std::forward(args)...); #else ::new ((void*)p) U(std::forward(args)...); #endif } #endif int& count_; }; struct A0 { TEST_CONSTEXPR_CXX20 A0(int* count) {++*count;} }; struct A1 { TEST_CONSTEXPR_CXX20 A1(int* count, char c) { assert(c == 'c'); ++*count; } }; struct A2 { TEST_CONSTEXPR_CXX20 A2(int* count, char c, int i) { assert(c == 'd'); assert(i == 5); ++*count; } }; TEST_CONSTEXPR_CXX20 bool test() { { int A0_count = 0; A a; std::allocator alloc; A0* a0 = alloc.allocate(1); assert(A0_count == 0); std::allocator_traits >::construct(a, a0, &A0_count); assert(A0_count == 1); alloc.deallocate(a0, 1); } { int A1_count = 0; A a; std::allocator alloc; A1* a1 = alloc.allocate(1); assert(A1_count == 0); std::allocator_traits >::construct(a, a1, &A1_count, 'c'); assert(A1_count == 1); alloc.deallocate(a1, 1); } { int A2_count = 0; A a; std::allocator alloc; A2* a2 = alloc.allocate(1); assert(A2_count == 0); std::allocator_traits >::construct(a, a2, &A2_count, 'd', 5); assert(A2_count == 1); alloc.deallocate(a2, 1); } { typedef IncompleteHolder* VT; typedef A Alloc; Alloc a; std::allocator alloc; VT* vt = alloc.allocate(1); std::allocator_traits::construct(a, vt, nullptr); alloc.deallocate(vt, 1); } #if TEST_STD_VER >= 11 { int A0_count = 0; int b_construct = 0; B b(b_construct); std::allocator alloc; A0* a0 = alloc.allocate(1); assert(A0_count == 0); assert(b_construct == 0); std::allocator_traits >::construct(b, a0, &A0_count); assert(A0_count == 1); assert(b_construct == 1); alloc.deallocate(a0, 1); } { int A1_count = 0; int b_construct = 0; B b(b_construct); std::allocator alloc; A1* a1 = alloc.allocate(1); assert(A1_count == 0); assert(b_construct == 0); std::allocator_traits >::construct(b, a1, &A1_count, 'c'); assert(A1_count == 1); assert(b_construct == 1); alloc.deallocate(a1, 1); } { int A2_count = 0; int b_construct = 0; B b(b_construct); std::allocator alloc; A2* a2 = alloc.allocate(1); assert(A2_count == 0); assert(b_construct == 0); std::allocator_traits >::construct(b, a2, &A2_count, 'd', 5); assert(A2_count == 1); assert(b_construct == 1); alloc.deallocate(a2, 1); } #endif return true; } int main(int, char**) { test(); #if TEST_STD_VER > 17 static_assert(test()); #endif return 0; }