From 10da596ac9f38ce272aad1375451f153fbe8bd54 Mon Sep 17 00:00:00 2001 From: MakersF Date: Sun, 5 Dec 2021 16:54:38 +0000 Subject: [PATCH] Add test case for object with templated PrintTo on generic type When PrintTo is defined on a set of types which are accepted with a compile time predicate (using SFINAE), the current implementation fails to compile due to an ambiguous error. --- googletest/test/googletest-printers-test.cc | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc index f708e9088..363d806c4 100644 --- a/googletest/test/googletest-printers-test.cc +++ b/googletest/test/googletest-printers-test.cc @@ -85,6 +85,15 @@ void PrintTo(EnumWithPrintTo e, std::ostream* os) { *os << (e == kEWPT1 ? "kEWPT1" : "invalid"); } +struct StructWithSFINAETemplatePrintToInGlobal {}; + +template ::value>::type> +void PrintTo(const T& obj, std::ostream* os) { + *os << "StructWithSFINAETemplatePrintToInGlobal"; +} + // A class implicitly convertible to BiggestInt. class BiggestIntConvertible { public: @@ -173,6 +182,15 @@ void PrintTo(const PrintableViaPrintToTemplate& x, ::std::ostream* os) { *os << "PrintableViaPrintToTemplate: " << x.value(); } +struct StructWithSFINAETemplatePrintToInFoo {}; + +template ::value>::type> +void PrintTo(const T& obj, std::ostream* os) { + *os << "StructWithSFINAETemplatePrintToInFoo"; +} + // A user-defined streamable class template in a user namespace. template class StreamableTemplateInFoo { @@ -1292,6 +1310,16 @@ TEST(PrintReferenceWrapper, Unprintable) { Print(std::cref(up))); } +TEST(PrintPrintableTypeWithSfinaePrintTo, InGlobalNamespace) { + EXPECT_EQ("StructWithSFINAETemplatePrintToInGlobal", + Print(StructWithSFINAETemplatePrintToInGlobal())); +} + +TEST(PrintPrintableTypeWithSfinaePrintTo, InFooNamespace) { + EXPECT_EQ("StructWithSFINAETemplatePrintToInFoo", + Print(StructWithSFINAETemplatePrintToInFoo())); +} + // Tests printing user-defined unprintable types. // Unprintable types in the global namespace.