Skip to content

Commit 33e6fd2

Browse files
ZXShadyhorenmar
authored andcommitted
Remove recursion when stringifying std::tuple
1 parent a58df2d commit 33e6fd2

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

src/catch2/catch_tostring.hpp

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -407,44 +407,38 @@ namespace Catch {
407407

408408
// Separate std::tuple specialization
409409
#if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER)
410-
#include <tuple>
410+
# include <tuple>
411+
# include <utility>
411412
namespace Catch {
412413
namespace Detail {
413-
template<
414-
typename Tuple,
415-
std::size_t N = 0,
416-
bool = (N < std::tuple_size<Tuple>::value)
417-
>
418-
struct TupleElementPrinter {
419-
static void print(const Tuple& tuple, std::ostream& os) {
420-
os << (N ? ", " : " ")
421-
<< ::Catch::Detail::stringify(std::get<N>(tuple));
422-
TupleElementPrinter<Tuple, N + 1>::print(tuple, os);
423-
}
424-
};
425-
426-
template<
427-
typename Tuple,
428-
std::size_t N
429-
>
430-
struct TupleElementPrinter<Tuple, N, false> {
431-
static void print(const Tuple&, std::ostream&) {}
432-
};
433-
434-
}
414+
template <typename Tuple, std::size_t... Is>
415+
void PrintTuple( const Tuple& tuple,
416+
std::ostream& os,
417+
std::index_sequence<Is...> ) {
418+
// 1 + Account for when the tuple is empty
419+
char a[1 + sizeof...( Is )] = {
420+
( ( os << ( Is ? ", " : " " )
421+
<< ::Catch::Detail::stringify( std::get<Is>( tuple ) ) ),
422+
'\0' )... };
423+
(void)a;
424+
}
435425

426+
} // namespace Detail
436427

437-
template<typename ...Types>
428+
template <typename... Types>
438429
struct StringMaker<std::tuple<Types...>> {
439-
static std::string convert(const std::tuple<Types...>& tuple) {
430+
static std::string convert( const std::tuple<Types...>& tuple ) {
440431
ReusableStringStream rss;
441432
rss << '{';
442-
Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, rss.get());
433+
Detail::PrintTuple(
434+
tuple,
435+
rss.get(),
436+
std::make_index_sequence<sizeof...( Types )>{} );
443437
rss << " }";
444438
return rss.str();
445439
}
446440
};
447-
}
441+
} // namespace Catch
448442
#endif // CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER
449443

450444
#if defined(CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER) && defined(CATCH_CONFIG_CPP17_VARIANT)

0 commit comments

Comments
 (0)