catch3
Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.
Clonegit clone https://git.progmasoft.com/catch3
Remove recursion when stringifying std::tuple
- Commit
33e6fd217aee285eca97bef175df716264088b8a- Author
- ZXShady ·
- Committer
- Martin Hořeňovský
- Parents
a58df2d7c59b
Changes
src/catch2/catch_tostring.hpp | 48 +++++++++++++++++++------------------------ 1 file 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)