63 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9 10// <memory>11 12// template <typename _Alloc>13// void __swap_allocator(_Alloc& __a1, _Alloc& __a2);14 15#include <__cxx03/__memory/swap_allocator.h>16#include <cassert>17#include <memory>18#include <utility>19 20#include "test_macros.h"21 22template <bool Propagate, bool Noexcept>23struct Alloc {24 int i = 0;25 Alloc() = default;26 Alloc(int set_i) : i(set_i) {}27 28 using value_type = int;29 using propagate_on_container_swap = std::integral_constant<bool, Propagate>;30 31 friend void swap(Alloc& a1, Alloc& a2) TEST_NOEXCEPT_COND(Noexcept) {32 std::swap(a1.i, a2.i);33 }34 35};36 37using PropagatingAlloc = Alloc</*Propagate=*/true, /*Noexcept=*/true>;38static_assert(std::allocator_traits<PropagatingAlloc>::propagate_on_container_swap::value, "");39 40using NonPropagatingAlloc = Alloc</*Propagate=*/false, /*Noexcept=*/true>;41static_assert(!std::allocator_traits<NonPropagatingAlloc>::propagate_on_container_swap::value, "");42 43using NoexceptSwapAlloc = Alloc</*Propagate=*/true, /*Noexcept=*/true>;44using ThrowingSwapAlloc = Alloc</*Propagate=*/true, /*Noexcept=*/false>;45 46int main(int, char**) {47 {48 PropagatingAlloc a1(1), a2(42);49 std::__swap_allocator(a1, a2);50 assert(a1.i == 42);51 assert(a2.i == 1);52 }53 54 {55 NonPropagatingAlloc a1(1), a2(42);56 std::__swap_allocator(a1, a2);57 assert(a1.i == 1);58 assert(a2.i == 42);59 }60 61 return 0;62}63