Remove recursion when stringifying std::tuple

This commit is contained in:
ZXShady
2025-09-12 00:52:11 +03:00
committed by Martin Hořeňovský
parent a58df2d7c5
commit 33e6fd217a

View File

@@ -408,43 +408,37 @@ namespace Catch {
// Separate std::tuple specialization // Separate std::tuple specialization
#if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER) #if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER)
# include <tuple> # include <tuple>
# include <utility>
namespace Catch { namespace Catch {
namespace Detail { namespace Detail {
template< template <typename Tuple, std::size_t... Is>
typename Tuple, void PrintTuple( const Tuple& tuple,
std::size_t N = 0, std::ostream& os,
bool = (N < std::tuple_size<Tuple>::value) std::index_sequence<Is...> ) {
> // 1 + Account for when the tuple is empty
struct TupleElementPrinter { char a[1 + sizeof...( Is )] = {
static void print(const Tuple& tuple, std::ostream& os) { ( ( os << ( Is ? ", " : " " )
os << (N ? ", " : " ") << ::Catch::Detail::stringify( std::get<Is>( tuple ) ) ),
<< ::Catch::Detail::stringify(std::get<N>(tuple)); '\0' )... };
TupleElementPrinter<Tuple, N + 1>::print(tuple, os); (void)a;
}
};
template<
typename Tuple,
std::size_t N
>
struct TupleElementPrinter<Tuple, N, false> {
static void print(const Tuple&, std::ostream&) {}
};
} }
} // namespace Detail
template <typename... Types> template <typename... Types>
struct StringMaker<std::tuple<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; ReusableStringStream rss;
rss << '{'; rss << '{';
Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, rss.get()); Detail::PrintTuple(
tuple,
rss.get(),
std::make_index_sequence<sizeof...( Types )>{} );
rss << " }"; rss << " }";
return rss.str(); return rss.str();
} }
}; };
} } // namespace Catch
#endif // CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER #endif // CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER
#if defined(CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER) && defined(CATCH_CONFIG_CPP17_VARIANT) #if defined(CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER) && defined(CATCH_CONFIG_CPP17_VARIANT)