diff --git a/include/CLI/TypeTools.hpp b/include/CLI/TypeTools.hpp index e2a0e87bb..3d8a2f953 100644 --- a/include/CLI/TypeTools.hpp +++ b/include/CLI/TypeTools.hpp @@ -10,6 +10,17 @@ #include #include +// [CLI11:verbatim] +#if defined(CLI11_CPP17) +#if defined(__has_include) +#if __has_include() +#include +#define CLI11_HAS_STRING_VIEW +#endif +#endif +#endif +// [CLI11:verbatim] + namespace CLI { // Type tools @@ -72,6 +83,10 @@ template struct IsMemberType { using type = T; }; /// The main custom type needed here is const char * should be a string. template <> struct IsMemberType { using type = std::string; }; +#ifdef CLI11_HAS_STRING_VIEW +template <> struct IsMemberType { using type = std::string; }; +#endif + namespace detail { // These are utilities for IsMember diff --git a/include/CLI/Validators.hpp b/include/CLI/Validators.hpp index 536f8a65d..96253a489 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -503,7 +503,7 @@ auto search(const T &set, const V &val, const std::function &filter_functi // if we haven't found it do the longer linear search with all the element translations auto &setref = detail::smart_deref(set); auto it = std::find_if(std::begin(setref), std::end(setref), [&](decltype(*std::begin(setref)) v) { - V a = detail::pair_adaptor::first(v); + V a{detail::pair_adaptor::first(v)}; a = filter_function(a); return (a == val); }); diff --git a/tests/TransformTest.cpp b/tests/TransformTest.cpp index 776e71db9..4884a3eea 100644 --- a/tests/TransformTest.cpp +++ b/tests/TransformTest.cpp @@ -102,6 +102,18 @@ TEST_F(TApp, SimpleTransformFn) { EXPECT_EQ(value, 1); } +#if defined(CLI11_HAS_STRING_VIEW) +TEST_F(TApp, StringViewTransformFn) { + std::string value; + std::map map = {// key length > std::string().capacity() [SSO length] + {"a-rather-long-argument", "mapped"}}; + app.add_option("-s", value)->transform(CLI::CheckedTransformer(map)); + args = {"-s", "a-rather-long-argument"}; + run(); + EXPECT_EQ(value, "mapped"); +} +#endif + TEST_F(TApp, SimpleNumericalTransformFn) { int value; auto opt =