From b42d76653f4c184132a187c4df9c859902f65806 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Fri, 24 Jan 2020 14:32:02 +0100 Subject: [PATCH] Non-recursive variant of detail::length() for compilers with maximum recursion depth in compile-time constexpr evaluation. --- include/nonstd/string_view.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/nonstd/string_view.hpp b/include/nonstd/string_view.hpp index c49994f..808a276 100644 --- a/include/nonstd/string_view.hpp +++ b/include/nonstd/string_view.hpp @@ -409,6 +409,22 @@ namespace nonstd { namespace sv_lite { namespace detail { +#if nssv_CPP14_OR_GREATER + +template< typename CharT > +inline constexpr std::size_t length( CharT * s, std::size_t result = 0 ) +{ + CharT * v = s; + std::size_t r = result; + while ( *v != '\0' ) { + ++v; + ++r; + } + return r; +} + +#else // nssv_CPP14_OR_GREATER + // Expect tail call optimization to make length() non-recursive: template< typename CharT > @@ -417,6 +433,8 @@ inline constexpr std::size_t length( CharT * s, std::size_t result = 0 ) return *s == '\0' ? result : length( s + 1, result + 1 ); } +#endif // nssv_CPP14_OR_GREATER + } // namespace detail #endif // nssv_CPP11_OR_GREATER