From a0c32abd7e7847d09bdf1f083f75aca5d9660566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rimas=20Misevi=C4=8Dius?= Date: Fri, 25 Oct 2024 22:03:31 +0300 Subject: [PATCH] Check in str_arg_char if StrT has size() member Before it was checked StrT has length() member. This allows other containers (e.g. std::vector) to be used as input. --- include/upa/str_arg.h | 16 ++++++++-------- test/test-str_arg.cpp | 6 ++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/include/upa/str_arg.h b/include/upa/str_arg.h index 8d3595c..ccb6e46 100644 --- a/include/upa/str_arg.h +++ b/include/upa/str_arg.h @@ -141,19 +141,19 @@ namespace detail { template auto test_data(long) -> void; - // test class T has length() member + // test class T has size() member template - auto test_length(int) -> decltype(std::declval().length()); + auto test_size(int) -> decltype(std::declval().size()); template - auto test_length(long) -> void; + auto test_size(long) -> void; // T::data() return type (void - if no such member) template using data_member_t = decltype(detail::test_data(0)); - // T::length() return type (void - if no such member) + // T::size() return type (void - if no such member) template - using length_member_t = decltype(detail::test_length(0)); + using size_member_t = decltype(detail::test_size(0)); } // namespace detail @@ -172,16 +172,16 @@ struct str_arg_char : std::remove_cv { } }; -// String that has data() and length() members +// String that has data() and size() members template struct str_arg_char : std::enable_if< std::is_pointer>::value && - is_size_type>::value, + is_size_type>::value, remove_cvptr_t>> { template static str_arg to_str_arg(const STR& str) { - return { str.data(), str.length() }; + return { str.data(), str.size() }; } }; diff --git a/test/test-str_arg.cpp b/test/test-str_arg.cpp index 76fbd8e..0351e0e 100644 --- a/test/test-str_arg.cpp +++ b/test/test-str_arg.cpp @@ -9,6 +9,8 @@ #include "upa/str_arg.h" //#include "doctest-main.h" +#include +#include // Function to test @@ -86,6 +88,10 @@ inline void test_char() { procfn(upa::str_arg{cptr, cptr + N}); procfn(upa::str_arg{vptr, vptr + N}); + // has data() and size() members + procfn(std::array{ '1', '2', '3'}); + procfn(std::vector{ '1', '2', '3'}); + // std::basic_string const std::basic_string str(arr); procfn(str);