diff options
author | Lawrence Wolf-Sonkin <lawrencews@google.com> | 2024-04-04 19:37:50 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-04-04 19:38:42 (GMT) |
commit | 0af976647f49ff0944c5971ae0a45d6fcdf1ecca (patch) | |
tree | 4c362e4f7f408c31fd6ed6f3364d21889e6040ff /googletest/include | |
parent | 61db1e1740a828d9df94fd167a9eb4137cd6def2 (diff) | |
download | googletest-0af976647f49ff0944c5971ae0a45d6fcdf1ecca.zip googletest-0af976647f49ff0944c5971ae0a45d6fcdf1ecca.tar.gz googletest-0af976647f49ff0944c5971ae0a45d6fcdf1ecca.tar.bz2 |
[gtest] Use `std::index_sequence` and friends instead of rolling our own
* Applies for `std::index_sequence`, `std::make_index_sequence`, and `std::index_sequence_for` replacing `IndexSequence`, `MakeIndexSequence` and IndexSequenceFor`
* Also deleted implementation helper `DoubleSequence`
* The standard interfaces [have been in the standard library since C++14](https://en.cppreference.com/w/cpp/utility/integer_sequence), which [is the minimum supported C++ version by Google Test](https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md)
PiperOrigin-RevId: 621939977
Change-Id: Id264266f08da66c0fa2a6e6fbb8f86fd3cb3a421
Diffstat (limited to 'googletest/include')
-rw-r--r-- | googletest/include/gtest/internal/gtest-internal.h | 54 | ||||
-rw-r--r-- | googletest/include/gtest/internal/gtest-param-util.h | 8 |
2 files changed, 14 insertions, 48 deletions
diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index 4661248..cffb8e1 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -1137,40 +1137,6 @@ class NativeArray { void (NativeArray::*clone_)(const Element*, size_t); }; -// Backport of std::index_sequence. -template <size_t... Is> -struct IndexSequence { - using type = IndexSequence; -}; - -// Double the IndexSequence, and one if plus_one is true. -template <bool plus_one, typename T, size_t sizeofT> -struct DoubleSequence; -template <size_t... I, size_t sizeofT> -struct DoubleSequence<true, IndexSequence<I...>, sizeofT> { - using type = IndexSequence<I..., (sizeofT + I)..., 2 * sizeofT>; -}; -template <size_t... I, size_t sizeofT> -struct DoubleSequence<false, IndexSequence<I...>, sizeofT> { - using type = IndexSequence<I..., (sizeofT + I)...>; -}; - -// Backport of std::make_index_sequence. -// It uses O(ln(N)) instantiation depth. -template <size_t N> -struct MakeIndexSequenceImpl - : DoubleSequence<N % 2 == 1, typename MakeIndexSequenceImpl<N / 2>::type, - N / 2>::type {}; - -template <> -struct MakeIndexSequenceImpl<0> : IndexSequence<> {}; - -template <size_t N> -using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::type; - -template <typename... T> -using IndexSequenceFor = typename MakeIndexSequence<sizeof...(T)>::type; - template <size_t> struct Ignore { Ignore(...); // NOLINT @@ -1179,7 +1145,7 @@ struct Ignore { template <typename> struct ElemFromListImpl; template <size_t... I> -struct ElemFromListImpl<IndexSequence<I...>> { +struct ElemFromListImpl<std::index_sequence<I...>> { // We make Ignore a template to solve a problem with MSVC. // A non-template Ignore would work fine with `decltype(Ignore(I))...`, but // MSVC doesn't understand how to deal with that pack expansion. @@ -1190,9 +1156,8 @@ struct ElemFromListImpl<IndexSequence<I...>> { template <size_t N, typename... T> struct ElemFromList { - using type = - decltype(ElemFromListImpl<typename MakeIndexSequence<N>::type>::Apply( - static_cast<T (*)()>(nullptr)...)); + using type = decltype(ElemFromListImpl<std::make_index_sequence<N>>::Apply( + static_cast<T (*)()>(nullptr)...)); }; struct FlatTupleConstructTag {}; @@ -1217,9 +1182,9 @@ template <typename Derived, typename Idx> struct FlatTupleBase; template <size_t... Idx, typename... T> -struct FlatTupleBase<FlatTuple<T...>, IndexSequence<Idx...>> +struct FlatTupleBase<FlatTuple<T...>, std::index_sequence<Idx...>> : FlatTupleElemBase<FlatTuple<T...>, Idx>... { - using Indices = IndexSequence<Idx...>; + using Indices = std::index_sequence<Idx...>; FlatTupleBase() = default; template <typename... Args> explicit FlatTupleBase(FlatTupleConstructTag, Args&&... args) @@ -1254,14 +1219,15 @@ struct FlatTupleBase<FlatTuple<T...>, IndexSequence<Idx...>> // implementations. // FlatTuple and ElemFromList are not recursive and have a fixed depth // regardless of T... -// MakeIndexSequence, on the other hand, it is recursive but with an +// std::make_index_sequence, on the other hand, it is recursive but with an // instantiation depth of O(ln(N)). template <typename... T> class FlatTuple : private FlatTupleBase<FlatTuple<T...>, - typename MakeIndexSequence<sizeof...(T)>::type> { - using Indices = typename FlatTupleBase< - FlatTuple<T...>, typename MakeIndexSequence<sizeof...(T)>::type>::Indices; + std::make_index_sequence<sizeof...(T)>> { + using Indices = + typename FlatTupleBase<FlatTuple<T...>, + std::make_index_sequence<sizeof...(T)>>::Indices; public: FlatTuple() = default; diff --git a/googletest/include/gtest/internal/gtest-param-util.h b/googletest/include/gtest/internal/gtest-param-util.h index 1fc500f..cc7ea53 100644 --- a/googletest/include/gtest/internal/gtest-param-util.h +++ b/googletest/include/gtest/internal/gtest-param-util.h @@ -807,12 +807,12 @@ class ValueArray { template <typename T> operator ParamGenerator<T>() const { // NOLINT - return ValuesIn(MakeVector<T>(MakeIndexSequence<sizeof...(Ts)>())); + return ValuesIn(MakeVector<T>(std::make_index_sequence<sizeof...(Ts)>())); } private: template <typename T, size_t... I> - std::vector<T> MakeVector(IndexSequence<I...>) const { + std::vector<T> MakeVector(std::index_sequence<I...>) const { return std::vector<T>{static_cast<T>(v_.template Get<I>())...}; } @@ -842,7 +842,7 @@ class CartesianProductGenerator template <class I> class IteratorImpl; template <size_t... I> - class IteratorImpl<IndexSequence<I...>> + class IteratorImpl<std::index_sequence<I...>> : public ParamIteratorInterface<ParamType> { public: IteratorImpl(const ParamGeneratorInterface<ParamType>* base, @@ -933,7 +933,7 @@ class CartesianProductGenerator std::shared_ptr<ParamType> current_value_; }; - using Iterator = IteratorImpl<typename MakeIndexSequence<sizeof...(T)>::type>; + using Iterator = IteratorImpl<std::make_index_sequence<sizeof...(T)>>; std::tuple<ParamGenerator<T>...> generators_; }; |