brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.4 KiB · 11f7105 Raw
121 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// <array>10 11// implicitly generated array constructors / assignment operators12 13#include <array>14#include <type_traits>15#include <cassert>16#include "test_macros.h"17 18// In C++03 the copy assignment operator is not deleted when the implicitly19// generated operator would be ill-formed; like in the case of a struct with a20// const member.21#if TEST_STD_VER < 1122#  define TEST_NOT_COPY_ASSIGNABLE(T) ((void)0)23#else24#  define TEST_NOT_COPY_ASSIGNABLE(T) static_assert(!std::is_copy_assignable<T>::value, "")25#endif26 27struct NoDefault {28  TEST_CONSTEXPR NoDefault(int) {}29};30 31struct NonTrivialCopy {32  TEST_CONSTEXPR NonTrivialCopy() {}33  TEST_CONSTEXPR NonTrivialCopy(NonTrivialCopy const&) {}34  TEST_CONSTEXPR_CXX14 NonTrivialCopy& operator=(NonTrivialCopy const&) { return *this; }35};36 37TEST_CONSTEXPR_CXX14 bool tests() {38  {39    typedef std::array<double, 3> Array;40    Array array = {1.1, 2.2, 3.3};41    Array copy  = array;42    copy        = array;43    static_assert(std::is_copy_constructible<Array>::value, "");44    static_assert(std::is_copy_assignable<Array>::value, "");45  }46  {47    typedef std::array<double const, 3> Array;48    Array array = {1.1, 2.2, 3.3};49    Array copy  = array;50    (void)copy;51    static_assert(std::is_copy_constructible<Array>::value, "");52    TEST_NOT_COPY_ASSIGNABLE(Array);53  }54  {55    typedef std::array<double, 0> Array;56    Array array = {};57    Array copy  = array;58    copy        = array;59    static_assert(std::is_copy_constructible<Array>::value, "");60    static_assert(std::is_copy_assignable<Array>::value, "");61  }62  {63    // const arrays of size 0 should disable the implicit copy assignment operator.64    typedef std::array<double const, 0> Array;65    Array array = {};66    Array copy  = array;67    (void)copy;68    static_assert(std::is_copy_constructible<Array>::value, "");69    TEST_NOT_COPY_ASSIGNABLE(Array);70  }71  {72    typedef std::array<NoDefault, 0> Array;73    Array array = {};74    Array copy  = array;75    copy        = array;76    static_assert(std::is_copy_constructible<Array>::value, "");77    static_assert(std::is_copy_assignable<Array>::value, "");78  }79  {80    typedef std::array<NoDefault const, 0> Array;81    Array array = {};82    Array copy  = array;83    (void)copy;84    static_assert(std::is_copy_constructible<Array>::value, "");85    TEST_NOT_COPY_ASSIGNABLE(Array);86  }87 88  // Make sure we can implicitly copy a std::array of a non-trivially copyable type89  {90    typedef std::array<NonTrivialCopy, 0> Array;91    Array array = {};92    Array copy  = array;93    copy        = array;94    static_assert(std::is_copy_constructible<Array>::value, "");95  }96  {97    typedef std::array<NonTrivialCopy, 1> Array;98    Array array = {};99    Array copy  = array;100    copy        = array;101    static_assert(std::is_copy_constructible<Array>::value, "");102  }103  {104    typedef std::array<NonTrivialCopy, 2> Array;105    Array array = {};106    Array copy  = array;107    copy        = array;108    static_assert(std::is_copy_constructible<Array>::value, "");109  }110 111  return true;112}113 114int main(int, char**) {115  tests();116#if TEST_STD_VER >= 14117  static_assert(tests(), "");118#endif119  return 0;120}121