diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index e18709c..cb20ee7 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -605,11 +605,10 @@ public: if constexpr (!details::IsContainer) { return get() == rhs; } else { - using ValueType = typename T::value_type; auto lhs = get(); return std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs), std::end(rhs), [](const auto &lhs, const auto &rhs) { - return std::any_cast(lhs) == rhs; + return lhs == rhs; }); } } @@ -856,16 +855,16 @@ private: * Get argument value given a type * @throws std::logic_error in case of incompatible types */ - template T get() const { + template auto get() const -> std::conditional_t, T, const T&> { if (!m_values.empty()) { if constexpr (details::IsContainer) { return any_cast_container(m_values); } else { - return std::any_cast(m_values.front()); + return *std::any_cast(&m_values.front()); } } if (m_default_value.has_value()) { - return std::any_cast(m_default_value); + return *std::any_cast(&m_default_value); } if constexpr (details::IsContainer) { if (!m_accepts_optional_like_value) { @@ -901,7 +900,7 @@ private: T result; std::transform( std::begin(operand), std::end(operand), std::back_inserter(result), - [](const auto &value) { return std::any_cast(value); }); + [](const auto &value) { return *std::any_cast(&value); }); return result; } @@ -1057,7 +1056,8 @@ public: * @throws std::logic_error if the option has no value * @throws std::bad_any_cast if the option is not of type T */ - template T get(std::string_view arg_name) const { + template auto get(std::string_view arg_name) const + -> std::conditional_t, T, const T&> { if (!m_is_parsed) { throw std::logic_error("Nothing parsed, no arguments are available."); }