Skip to content

Commit 0af9766

Browse files
lwolfsonkincopybara-github
authored andcommitted
[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
1 parent 61db1e1 commit 0af9766

File tree

7 files changed

+56
-103
lines changed

7 files changed

+56
-103
lines changed

googlemock/include/gmock/gmock-actions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,13 +2135,13 @@ struct ActionImpl<R(Args...), Impl> : ImplBase<Impl>::type {
21352135
R operator()(Args&&... arg) const {
21362136
static constexpr size_t kMaxArgs =
21372137
sizeof...(Args) <= 10 ? sizeof...(Args) : 10;
2138-
return Apply(MakeIndexSequence<kMaxArgs>{},
2139-
MakeIndexSequence<10 - kMaxArgs>{},
2138+
return Apply(std::make_index_sequence<kMaxArgs>{},
2139+
std::make_index_sequence<10 - kMaxArgs>{},
21402140
args_type{std::forward<Args>(arg)...});
21412141
}
21422142

21432143
template <std::size_t... arg_id, std::size_t... excess_id>
2144-
R Apply(IndexSequence<arg_id...>, IndexSequence<excess_id...>,
2144+
R Apply(std::index_sequence<arg_id...>, std::index_sequence<excess_id...>,
21452145
const args_type& args) const {
21462146
// Impl need not be specific to the signature of action being implemented;
21472147
// only the implementing function body needs to have all of the specific

googlemock/include/gmock/gmock-matchers.h

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,12 @@ class MatcherBaseImpl<Derived<Ts...>> {
490490

491491
template <typename F>
492492
operator ::testing::Matcher<F>() const { // NOLINT(runtime/explicit)
493-
return Apply<F>(MakeIndexSequence<sizeof...(Ts)>{});
493+
return Apply<F>(std::make_index_sequence<sizeof...(Ts)>{});
494494
}
495495

496496
private:
497497
template <typename F, std::size_t... tuple_ids>
498-
::testing::Matcher<F> Apply(IndexSequence<tuple_ids...>) const {
498+
::testing::Matcher<F> Apply(std::index_sequence<tuple_ids...>) const {
499499
return ::testing::Matcher<F>(
500500
new typename Derived<Ts...>::template gmock_Impl<F>(
501501
std::get<tuple_ids>(params_)...));
@@ -3152,115 +3152,115 @@ class PairMatcher {
31523152
};
31533153

31543154
template <typename T, size_t... I>
3155-
auto UnpackStructImpl(const T& t, IndexSequence<I...>, int)
3156-
-> decltype(std::tie(get<I>(t)...)) {
3155+
auto UnpackStructImpl(const T& t, std::index_sequence<I...>,
3156+
int) -> decltype(std::tie(get<I>(t)...)) {
31573157
static_assert(std::tuple_size<T>::value == sizeof...(I),
31583158
"Number of arguments doesn't match the number of fields.");
31593159
return std::tie(get<I>(t)...);
31603160
}
31613161

31623162
#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
31633163
template <typename T>
3164-
auto UnpackStructImpl(const T& t, MakeIndexSequence<1>, char) {
3164+
auto UnpackStructImpl(const T& t, std::make_index_sequence<1>, char) {
31653165
const auto& [a] = t;
31663166
return std::tie(a);
31673167
}
31683168
template <typename T>
3169-
auto UnpackStructImpl(const T& t, MakeIndexSequence<2>, char) {
3169+
auto UnpackStructImpl(const T& t, std::make_index_sequence<2>, char) {
31703170
const auto& [a, b] = t;
31713171
return std::tie(a, b);
31723172
}
31733173
template <typename T>
3174-
auto UnpackStructImpl(const T& t, MakeIndexSequence<3>, char) {
3174+
auto UnpackStructImpl(const T& t, std::make_index_sequence<3>, char) {
31753175
const auto& [a, b, c] = t;
31763176
return std::tie(a, b, c);
31773177
}
31783178
template <typename T>
3179-
auto UnpackStructImpl(const T& t, MakeIndexSequence<4>, char) {
3179+
auto UnpackStructImpl(const T& t, std::make_index_sequence<4>, char) {
31803180
const auto& [a, b, c, d] = t;
31813181
return std::tie(a, b, c, d);
31823182
}
31833183
template <typename T>
3184-
auto UnpackStructImpl(const T& t, MakeIndexSequence<5>, char) {
3184+
auto UnpackStructImpl(const T& t, std::make_index_sequence<5>, char) {
31853185
const auto& [a, b, c, d, e] = t;
31863186
return std::tie(a, b, c, d, e);
31873187
}
31883188
template <typename T>
3189-
auto UnpackStructImpl(const T& t, MakeIndexSequence<6>, char) {
3189+
auto UnpackStructImpl(const T& t, std::make_index_sequence<6>, char) {
31903190
const auto& [a, b, c, d, e, f] = t;
31913191
return std::tie(a, b, c, d, e, f);
31923192
}
31933193
template <typename T>
3194-
auto UnpackStructImpl(const T& t, MakeIndexSequence<7>, char) {
3194+
auto UnpackStructImpl(const T& t, std::make_index_sequence<7>, char) {
31953195
const auto& [a, b, c, d, e, f, g] = t;
31963196
return std::tie(a, b, c, d, e, f, g);
31973197
}
31983198
template <typename T>
3199-
auto UnpackStructImpl(const T& t, MakeIndexSequence<8>, char) {
3199+
auto UnpackStructImpl(const T& t, std::make_index_sequence<8>, char) {
32003200
const auto& [a, b, c, d, e, f, g, h] = t;
32013201
return std::tie(a, b, c, d, e, f, g, h);
32023202
}
32033203
template <typename T>
3204-
auto UnpackStructImpl(const T& t, MakeIndexSequence<9>, char) {
3204+
auto UnpackStructImpl(const T& t, std::make_index_sequence<9>, char) {
32053205
const auto& [a, b, c, d, e, f, g, h, i] = t;
32063206
return std::tie(a, b, c, d, e, f, g, h, i);
32073207
}
32083208
template <typename T>
3209-
auto UnpackStructImpl(const T& t, MakeIndexSequence<10>, char) {
3209+
auto UnpackStructImpl(const T& t, std::make_index_sequence<10>, char) {
32103210
const auto& [a, b, c, d, e, f, g, h, i, j] = t;
32113211
return std::tie(a, b, c, d, e, f, g, h, i, j);
32123212
}
32133213
template <typename T>
3214-
auto UnpackStructImpl(const T& t, MakeIndexSequence<11>, char) {
3214+
auto UnpackStructImpl(const T& t, std::make_index_sequence<11>, char) {
32153215
const auto& [a, b, c, d, e, f, g, h, i, j, k] = t;
32163216
return std::tie(a, b, c, d, e, f, g, h, i, j, k);
32173217
}
32183218
template <typename T>
3219-
auto UnpackStructImpl(const T& t, MakeIndexSequence<12>, char) {
3219+
auto UnpackStructImpl(const T& t, std::make_index_sequence<12>, char) {
32203220
const auto& [a, b, c, d, e, f, g, h, i, j, k, l] = t;
32213221
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l);
32223222
}
32233223
template <typename T>
3224-
auto UnpackStructImpl(const T& t, MakeIndexSequence<13>, char) {
3224+
auto UnpackStructImpl(const T& t, std::make_index_sequence<13>, char) {
32253225
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m] = t;
32263226
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m);
32273227
}
32283228
template <typename T>
3229-
auto UnpackStructImpl(const T& t, MakeIndexSequence<14>, char) {
3229+
auto UnpackStructImpl(const T& t, std::make_index_sequence<14>, char) {
32303230
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n] = t;
32313231
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
32323232
}
32333233
template <typename T>
3234-
auto UnpackStructImpl(const T& t, MakeIndexSequence<15>, char) {
3234+
auto UnpackStructImpl(const T& t, std::make_index_sequence<15>, char) {
32353235
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o] = t;
32363236
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
32373237
}
32383238
template <typename T>
3239-
auto UnpackStructImpl(const T& t, MakeIndexSequence<16>, char) {
3239+
auto UnpackStructImpl(const T& t, std::make_index_sequence<16>, char) {
32403240
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] = t;
32413241
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p);
32423242
}
32433243
template <typename T>
3244-
auto UnpackStructImpl(const T& t, MakeIndexSequence<17>, char) {
3244+
auto UnpackStructImpl(const T& t, std::make_index_sequence<17>, char) {
32453245
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q] = t;
32463246
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
32473247
}
32483248
template <typename T>
3249-
auto UnpackStructImpl(const T& t, MakeIndexSequence<18>, char) {
3249+
auto UnpackStructImpl(const T& t, std::make_index_sequence<18>, char) {
32503250
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r] = t;
32513251
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r);
32523252
}
32533253
template <typename T>
3254-
auto UnpackStructImpl(const T& t, MakeIndexSequence<19>, char) {
3254+
auto UnpackStructImpl(const T& t, std::make_index_sequence<19>, char) {
32553255
const auto& [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s] = t;
32563256
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s);
32573257
}
32583258
#endif // defined(__cpp_structured_bindings)
32593259

32603260
template <size_t I, typename T>
32613261
auto UnpackStruct(const T& t)
3262-
-> decltype((UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0)) {
3263-
return (UnpackStructImpl)(t, MakeIndexSequence<I>{}, 0);
3262+
-> decltype((UnpackStructImpl)(t, std::make_index_sequence<I>{}, 0)) {
3263+
return (UnpackStructImpl)(t, std::make_index_sequence<I>{}, 0);
32643264
}
32653265

32663266
// Helper function to do comma folding in C++11.
@@ -3273,7 +3273,7 @@ template <typename Struct, typename StructSize>
32733273
class FieldsAreMatcherImpl;
32743274

32753275
template <typename Struct, size_t... I>
3276-
class FieldsAreMatcherImpl<Struct, IndexSequence<I...>>
3276+
class FieldsAreMatcherImpl<Struct, std::index_sequence<I...>>
32773277
: public MatcherInterface<Struct> {
32783278
using UnpackedType =
32793279
decltype(UnpackStruct<sizeof...(I)>(std::declval<const Struct&>()));
@@ -3355,8 +3355,8 @@ class FieldsAreMatcher {
33553355
template <typename Struct>
33563356
operator Matcher<Struct>() const { // NOLINT
33573357
return Matcher<Struct>(
3358-
new FieldsAreMatcherImpl<const Struct&, IndexSequenceFor<Inner...>>(
3359-
matchers_));
3358+
new FieldsAreMatcherImpl<const Struct&,
3359+
std::index_sequence_for<Inner...>>(matchers_));
33603360
}
33613361

33623362
private:

googlemock/include/gmock/internal/gmock-internal-utils.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <ostream> // NOLINT
4545
#include <string>
4646
#include <type_traits>
47+
#include <utility>
4748
#include <vector>
4849

4950
#include "gmock/internal/gmock-port.h"
@@ -420,20 +421,21 @@ struct RemoveConstFromKey<std::pair<const K, V> > {
420421
GTEST_API_ void IllegalDoDefault(const char* file, int line);
421422

422423
template <typename F, typename Tuple, size_t... Idx>
423-
auto ApplyImpl(F&& f, Tuple&& args, IndexSequence<Idx...>)
424+
auto ApplyImpl(F&& f, Tuple&& args, std::index_sequence<Idx...>)
424425
-> decltype(std::forward<F>(f)(
425426
std::get<Idx>(std::forward<Tuple>(args))...)) {
426427
return std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(args))...);
427428
}
428429

429430
// Apply the function to a tuple of arguments.
430431
template <typename F, typename Tuple>
431-
auto Apply(F&& f, Tuple&& args) -> decltype(ApplyImpl(
432-
std::forward<F>(f), std::forward<Tuple>(args),
433-
MakeIndexSequence<std::tuple_size<
434-
typename std::remove_reference<Tuple>::type>::value>())) {
432+
auto Apply(F&& f, Tuple&& args)
433+
-> decltype(ApplyImpl(
434+
std::forward<F>(f), std::forward<Tuple>(args),
435+
std::make_index_sequence<std::tuple_size<
436+
typename std::remove_reference<Tuple>::type>::value>())) {
435437
return ApplyImpl(std::forward<F>(f), std::forward<Tuple>(args),
436-
MakeIndexSequence<std::tuple_size<
438+
std::make_index_sequence<std::tuple_size<
437439
typename std::remove_reference<Tuple>::type>::value>());
438440
}
439441

googlemock/src/gmock-internal-utils.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <iostream>
4545
#include <ostream> // NOLINT
4646
#include <string>
47+
#include <utility>
4748
#include <vector>
4849

4950
#include "gmock/gmock.h"
@@ -211,14 +212,14 @@ constexpr char UnBase64Impl(char c, const char* const base64, char carry) {
211212
}
212213

213214
template <size_t... I>
214-
constexpr std::array<char, 256> UnBase64Impl(IndexSequence<I...>,
215+
constexpr std::array<char, 256> UnBase64Impl(std::index_sequence<I...>,
215216
const char* const base64) {
216217
return {
217218
{UnBase64Impl(UndoWebSafeEncoding(static_cast<char>(I)), base64, 0)...}};
218219
}
219220

220221
constexpr std::array<char, 256> UnBase64(const char* const base64) {
221-
return UnBase64Impl(MakeIndexSequence<256>{}, base64);
222+
return UnBase64Impl(std::make_index_sequence<256>{}, base64);
222223
}
223224

224225
static constexpr char kBase64[] =

googletest/include/gtest/internal/gtest-internal.h

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,40 +1137,6 @@ class NativeArray {
11371137
void (NativeArray::*clone_)(const Element*, size_t);
11381138
};
11391139

1140-
// Backport of std::index_sequence.
1141-
template <size_t... Is>
1142-
struct IndexSequence {
1143-
using type = IndexSequence;
1144-
};
1145-
1146-
// Double the IndexSequence, and one if plus_one is true.
1147-
template <bool plus_one, typename T, size_t sizeofT>
1148-
struct DoubleSequence;
1149-
template <size_t... I, size_t sizeofT>
1150-
struct DoubleSequence<true, IndexSequence<I...>, sizeofT> {
1151-
using type = IndexSequence<I..., (sizeofT + I)..., 2 * sizeofT>;
1152-
};
1153-
template <size_t... I, size_t sizeofT>
1154-
struct DoubleSequence<false, IndexSequence<I...>, sizeofT> {
1155-
using type = IndexSequence<I..., (sizeofT + I)...>;
1156-
};
1157-
1158-
// Backport of std::make_index_sequence.
1159-
// It uses O(ln(N)) instantiation depth.
1160-
template <size_t N>
1161-
struct MakeIndexSequenceImpl
1162-
: DoubleSequence<N % 2 == 1, typename MakeIndexSequenceImpl<N / 2>::type,
1163-
N / 2>::type {};
1164-
1165-
template <>
1166-
struct MakeIndexSequenceImpl<0> : IndexSequence<> {};
1167-
1168-
template <size_t N>
1169-
using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::type;
1170-
1171-
template <typename... T>
1172-
using IndexSequenceFor = typename MakeIndexSequence<sizeof...(T)>::type;
1173-
11741140
template <size_t>
11751141
struct Ignore {
11761142
Ignore(...); // NOLINT
@@ -1179,7 +1145,7 @@ struct Ignore {
11791145
template <typename>
11801146
struct ElemFromListImpl;
11811147
template <size_t... I>
1182-
struct ElemFromListImpl<IndexSequence<I...>> {
1148+
struct ElemFromListImpl<std::index_sequence<I...>> {
11831149
// We make Ignore a template to solve a problem with MSVC.
11841150
// A non-template Ignore would work fine with `decltype(Ignore(I))...`, but
11851151
// MSVC doesn't understand how to deal with that pack expansion.
@@ -1190,9 +1156,8 @@ struct ElemFromListImpl<IndexSequence<I...>> {
11901156

11911157
template <size_t N, typename... T>
11921158
struct ElemFromList {
1193-
using type =
1194-
decltype(ElemFromListImpl<typename MakeIndexSequence<N>::type>::Apply(
1195-
static_cast<T (*)()>(nullptr)...));
1159+
using type = decltype(ElemFromListImpl<std::make_index_sequence<N>>::Apply(
1160+
static_cast<T (*)()>(nullptr)...));
11961161
};
11971162

11981163
struct FlatTupleConstructTag {};
@@ -1217,9 +1182,9 @@ template <typename Derived, typename Idx>
12171182
struct FlatTupleBase;
12181183

12191184
template <size_t... Idx, typename... T>
1220-
struct FlatTupleBase<FlatTuple<T...>, IndexSequence<Idx...>>
1185+
struct FlatTupleBase<FlatTuple<T...>, std::index_sequence<Idx...>>
12211186
: FlatTupleElemBase<FlatTuple<T...>, Idx>... {
1222-
using Indices = IndexSequence<Idx...>;
1187+
using Indices = std::index_sequence<Idx...>;
12231188
FlatTupleBase() = default;
12241189
template <typename... Args>
12251190
explicit FlatTupleBase(FlatTupleConstructTag, Args&&... args)
@@ -1254,14 +1219,15 @@ struct FlatTupleBase<FlatTuple<T...>, IndexSequence<Idx...>>
12541219
// implementations.
12551220
// FlatTuple and ElemFromList are not recursive and have a fixed depth
12561221
// regardless of T...
1257-
// MakeIndexSequence, on the other hand, it is recursive but with an
1222+
// std::make_index_sequence, on the other hand, it is recursive but with an
12581223
// instantiation depth of O(ln(N)).
12591224
template <typename... T>
12601225
class FlatTuple
12611226
: private FlatTupleBase<FlatTuple<T...>,
1262-
typename MakeIndexSequence<sizeof...(T)>::type> {
1263-
using Indices = typename FlatTupleBase<
1264-
FlatTuple<T...>, typename MakeIndexSequence<sizeof...(T)>::type>::Indices;
1227+
std::make_index_sequence<sizeof...(T)>> {
1228+
using Indices =
1229+
typename FlatTupleBase<FlatTuple<T...>,
1230+
std::make_index_sequence<sizeof...(T)>>::Indices;
12651231

12661232
public:
12671233
FlatTuple() = default;

googletest/include/gtest/internal/gtest-param-util.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,12 +807,12 @@ class ValueArray {
807807

808808
template <typename T>
809809
operator ParamGenerator<T>() const { // NOLINT
810-
return ValuesIn(MakeVector<T>(MakeIndexSequence<sizeof...(Ts)>()));
810+
return ValuesIn(MakeVector<T>(std::make_index_sequence<sizeof...(Ts)>()));
811811
}
812812

813813
private:
814814
template <typename T, size_t... I>
815-
std::vector<T> MakeVector(IndexSequence<I...>) const {
815+
std::vector<T> MakeVector(std::index_sequence<I...>) const {
816816
return std::vector<T>{static_cast<T>(v_.template Get<I>())...};
817817
}
818818

@@ -842,7 +842,7 @@ class CartesianProductGenerator
842842
template <class I>
843843
class IteratorImpl;
844844
template <size_t... I>
845-
class IteratorImpl<IndexSequence<I...>>
845+
class IteratorImpl<std::index_sequence<I...>>
846846
: public ParamIteratorInterface<ParamType> {
847847
public:
848848
IteratorImpl(const ParamGeneratorInterface<ParamType>* base,
@@ -933,7 +933,7 @@ class CartesianProductGenerator
933933
std::shared_ptr<ParamType> current_value_;
934934
};
935935

936-
using Iterator = IteratorImpl<typename MakeIndexSequence<sizeof...(T)>::type>;
936+
using Iterator = IteratorImpl<std::make_index_sequence<sizeof...(T)>>;
937937

938938
std::tuple<ParamGenerator<T>...> generators_;
939939
};

0 commit comments

Comments
 (0)