Index: libc/src/__support/CPP/CMakeLists.txt =================================================================== --- libc/src/__support/CPP/CMakeLists.txt +++ libc/src/__support/CPP/CMakeLists.txt @@ -10,6 +10,7 @@ Limits.h StringView.h TypeTraits.h + Utility.h ) if(LLVM_LIBC_INCLUDE_SCUDO OR NOT LLVM_LIBC_FULL_BUILD) Index: libc/src/__support/CPP/Utility.h =================================================================== --- /dev/null +++ libc/src/__support/CPP/Utility.h @@ -0,0 +1,39 @@ +//===-- Analogous to ----------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_UTILITY_H +#define LLVM_LIBC_SRC_SUPPORT_CPP_UTILITY_H + +#include "src/__support/CPP/TypeTraits.h" + +namespace __llvm_libc::cpp { + +template struct IntegerSequence { + static_assert(IsIntegral::Value); + template using append = IntegerSequence; +}; + +namespace internal { + +template struct MakeIntegerSequence { + using type = typename MakeIntegerSequence::type::template append; +}; + +template struct MakeIntegerSequence { + using type = IntegerSequence; +}; + +} // namespace internal + +template +using MakeIntegerSequence = + typename internal::MakeIntegerSequence::type; + +} // namespace __llvm_libc::cpp + +#endif // LLVM_LIBC_SRC_SUPPORT_CPP_UTILITY_H Index: libc/test/utils/CPP/CMakeLists.txt =================================================================== --- libc/test/utils/CPP/CMakeLists.txt +++ libc/test/utils/CPP/CMakeLists.txt @@ -51,3 +51,13 @@ libc.src.__support.CPP.vector ) endif() + +add_libc_unittest( + int_seq_test + SUITE + libc_cpp_utils_unittests + SRCS + integer_sequence_test.cpp + DEPENDS + libc.src.__support.CPP.standalone_cpp +) Index: libc/test/utils/CPP/integer_sequence_test.cpp =================================================================== --- /dev/null +++ libc/test/utils/CPP/integer_sequence_test.cpp @@ -0,0 +1,33 @@ +//===-- Unittests for IntegerSequence -------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/__support/CPP/Utility.h" +#include "utils/UnitTest/Test.h" + +using namespace __llvm_libc::cpp; + +TEST(LlvmLibcIntegerSequencetTest, Basic) { + EXPECT_TRUE((IsSameV, MakeIntegerSequence>)); + EXPECT_TRUE((IsSameV, MakeIntegerSequence>)); + EXPECT_TRUE((IsSameV, MakeIntegerSequence>)); +} + +template +bool checkArray(IntegerSequence seq) { + T arr[sizeof...(Ts)]{Ts...}; + + for (T i = 0; i < static_cast(sizeof...(Ts)); i++) + if (arr[i] != i) + return false; + + return true; +} + +TEST(LlvmLibcIntegerSequencetTest, Many) { + EXPECT_TRUE(checkArray(MakeIntegerSequence{})); +}