Skip to content

Commit

Permalink
add the zero-based index of the alternative held by the variant
Browse files Browse the repository at this point in the history
Useful when distinguishing repeated alternative types,
e.g. std::variant<int, int>.
  • Loading branch information
kuzkry committed May 29, 2020
1 parent 8bd8aea commit 70d7f04
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions googletest/include/gtest/gtest-printers.h
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,9 @@ class UniversalPrinter<Variant<T...>> {
static void Print(const Variant<T...>& value, ::std::ostream* os) {
*os << '(';
#if __cplusplus >= 201703L
std::visit(Visitor{os}, value);
std::visit(Visitor{os, value.index()}, value);
#else
absl::visit(Visitor{os}, value);
absl::visit(Visitor{os, value.index()}, value);
#endif // __cplusplus >= 201703L
*os << ')';
}
Expand All @@ -747,10 +747,11 @@ class UniversalPrinter<Variant<T...>> {
struct Visitor {
template <typename U>
void operator()(const U& u) const {
*os << "'" << GetTypeName<U>() << "' with value ";
*os << "'" << GetTypeName<U>() << "(" << index << ")' with value ";
UniversalPrint(u, os);
}
::std::ostream* os;
std::size_t index;
};
};

Expand Down
6 changes: 3 additions & 3 deletions googletest/test/googletest-printers-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1582,11 +1582,11 @@ struct NonPrintable {

TEST(PrintOneofTest, Basic) {
using Type = internal::Variant<int, StreamableInGlobal, NonPrintable>;
EXPECT_EQ("('int' with value 7)", PrintToString(Type(7)));
EXPECT_EQ("('StreamableInGlobal' with value StreamableInGlobal)",
EXPECT_EQ("('int(0)' with value 7)", PrintToString(Type(7)));
EXPECT_EQ("('StreamableInGlobal(1)' with value StreamableInGlobal)",
PrintToString(Type(StreamableInGlobal{})));
EXPECT_EQ(
"('testing::gtest_printers_test::NonPrintable' with value 1-byte object "
"('testing::gtest_printers_test::NonPrintable(2)' with value 1-byte object "
"<11>)",
PrintToString(Type(NonPrintable{})));
}
Expand Down

0 comments on commit 70d7f04

Please sign in to comment.