brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.4 KiB · 84742f7 Raw
85 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// UNSUPPORTED: c++03, c++11, c++1410 11// type_traits12 13// is_swappable14 15#include <type_traits>16#include <vector>17#include "test_macros.h"18 19namespace MyNS {20 21// Make the test types non-copyable so that generic std::swap is not valid.22struct A {23  A(A const&) = delete;24  A& operator=(A const&) = delete;25};26 27struct B {28  B(B const&) = delete;29  B& operator=(B const&) = delete;30};31 32void swap(A&, A&) noexcept {}33void swap(B&, B&) {}34 35struct M {36  M(M const&) = delete;37  M& operator=(M const&) = delete;38};39 40void swap(M&&, M&&) noexcept {}41 42struct ThrowingMove {43    ThrowingMove(ThrowingMove&&) {}44    ThrowingMove& operator=(ThrowingMove&&) { return *this; }45};46 47} // namespace MyNS48 49int main(int, char**)50{51    using namespace MyNS;52    {53        // Test that is_swappable applies an lvalue reference to the type.54        static_assert(std::is_nothrow_swappable<int>::value, "");55        static_assert(std::is_nothrow_swappable<int&>::value, "");56        static_assert(!std::is_nothrow_swappable<M>::value, "");57        static_assert(!std::is_nothrow_swappable<M&&>::value, "");58    }59    {60        // Test that it correctly deduces the noexcept of swap.61        static_assert(std::is_nothrow_swappable<A>::value, "");62        static_assert(!std::is_nothrow_swappable<B>::value63                      && std::is_swappable<B>::value, "");64        static_assert(!std::is_nothrow_swappable<ThrowingMove>::value65                      && std::is_swappable<ThrowingMove>::value, "");66    }67    {68        // Test that it doesn't drop the qualifiers69        static_assert(!std::is_nothrow_swappable<const A>::value, "");70    }71    {72        // test non-referenceable types73        static_assert(!std::is_nothrow_swappable<void>::value, "");74        static_assert(!std::is_nothrow_swappable<int() const>::value, "");75        static_assert(!std::is_nothrow_swappable<int(int, ...) const &>::value, "");76    }77    {78        // test for presence of is_nothrow_swappable_v79        static_assert(std::is_nothrow_swappable_v<int>, "");80        static_assert(!std::is_nothrow_swappable_v<void>, "");81    }82 83  return 0;84}85