diff options
| author | ZXShady | 2025-09-12 00:52:11 +0300 |
|---|---|---|
| committer | Martin Hořeňovský | 2025-10-04 22:10:36 +0200 |
| commit | 33e6fd217aee285eca97bef175df716264088b8a (patch) | |
| tree | 5865a3ca9f7ff8e77c50041cc7179730ab39a208 | |
| parent | a58df2d7c59b037adca33c66c28ac63323d33cd4 (diff) | |
| download | catch3-33e6fd217aee285eca97bef175df716264088b8a.tar.gz | |
Remove recursion when stringifying std::tuple
| -rw-r--r-- | src/catch2/catch_tostring.hpp | 48 |
1 files changed, 21 insertions, 27 deletions
diff --git a/src/catch2/catch_tostring.hpp b/src/catch2/catch_tostring.hpp index db0338df..b005212f 100644 --- a/src/catch2/catch_tostring.hpp +++ b/src/catch2/catch_tostring.hpp @@ -407,44 +407,38 @@ namespace Catch { // Separate std::tuple specialization #if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER) -#include <tuple> +# include <tuple> +# include <utility> namespace Catch { namespace Detail { - template< - typename Tuple, - std::size_t N = 0, - bool = (N < std::tuple_size<Tuple>::value) - > - struct TupleElementPrinter { - static void print(const Tuple& tuple, std::ostream& os) { - os << (N ? ", " : " ") - << ::Catch::Detail::stringify(std::get<N>(tuple)); - TupleElementPrinter<Tuple, N + 1>::print(tuple, os); - } - }; - - template< - typename Tuple, - std::size_t N - > - struct TupleElementPrinter<Tuple, N, false> { - static void print(const Tuple&, std::ostream&) {} - }; - - } + template <typename Tuple, std::size_t... Is> + void PrintTuple( const Tuple& tuple, + std::ostream& os, + std::index_sequence<Is...> ) { + // 1 + Account for when the tuple is empty + char a[1 + sizeof...( Is )] = { + ( ( os << ( Is ? ", " : " " ) + << ::Catch::Detail::stringify( std::get<Is>( tuple ) ) ), + '\0' )... }; + (void)a; + } + } // namespace Detail - template<typename ...Types> + template <typename... Types> struct StringMaker<std::tuple<Types...>> { - static std::string convert(const std::tuple<Types...>& tuple) { + static std::string convert( const std::tuple<Types...>& tuple ) { ReusableStringStream rss; rss << '{'; - Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, rss.get()); + Detail::PrintTuple( + tuple, + rss.get(), + std::make_index_sequence<sizeof...( Types )>{} ); rss << " }"; return rss.str(); } }; -} +} // namespace Catch #endif // CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER #if defined(CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER) && defined(CATCH_CONFIG_CPP17_VARIANT) |