52 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// Make sure std::array<T, N> is trivially copyable whenever T is trivially copyable.10// This is not technically mandated by the Standard, but libc++ has been providing11// this property.12 13#include <array>14#include <type_traits>15 16struct Empty {};17 18struct TrivialCopy {19 int i;20 double j;21};22 23struct NonTrivialCopy {24 NonTrivialCopy(NonTrivialCopy const&) {}25 NonTrivialCopy& operator=(NonTrivialCopy const&) { return *this; }26};27 28template <typename T>29void check_trivially_copyable() {30 static_assert(std::is_trivially_copyable<std::array<T, 0> >::value, "");31 static_assert(std::is_trivially_copyable<std::array<T, 1> >::value, "");32 static_assert(std::is_trivially_copyable<std::array<T, 2> >::value, "");33 static_assert(std::is_trivially_copyable<std::array<T, 3> >::value, "");34}35 36int main(int, char**) {37 check_trivially_copyable<int>();38 check_trivially_copyable<long>();39 check_trivially_copyable<double>();40 check_trivially_copyable<long double>();41 check_trivially_copyable<Empty>();42 check_trivially_copyable<TrivialCopy>();43 44 // Check that std::array<T, 0> is still trivially copyable when T is not45 static_assert(std::is_trivially_copyable<std::array<NonTrivialCopy, 0> >::value, "");46 static_assert(!std::is_trivially_copyable<std::array<NonTrivialCopy, 1> >::value, "");47 static_assert(!std::is_trivially_copyable<std::array<NonTrivialCopy, 2> >::value, "");48 static_assert(!std::is_trivially_copyable<std::array<NonTrivialCopy, 3> >::value, "");49 50 return 0;51}52