From 6974f46851064e2425290f9081a96c56993d0db6 Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Tue, 17 Jan 2023 13:57:16 -0700 Subject: [PATCH] Revert "Use references for `any_cast`" This attempts to fix Issue #225-1 by reverting the change that turned a std::bad_any_cast exception into a nullptr. Reverts commit 357068156e. Signed-off-by: Sean Robinson --- include/argparse/argparse.hpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index ed02ddf..bfede16 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -699,10 +699,13 @@ 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 &a, const auto &b) { return a == b; }); + [](const auto &a, const auto &b) { + return std::any_cast(a) == b; + }); } } @@ -969,18 +972,16 @@ private: * Get argument value given a type * @throws std::logic_error in case of incompatible types */ - template - auto get() const - -> std::conditional_t, T, const T &> { + template T get() const { 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) { @@ -1016,7 +1017,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; } @@ -1247,9 +1248,7 @@ 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 - auto get(std::string_view arg_name) const - -> std::conditional_t, T, const T &> { + template T get(std::string_view arg_name) const { if (!m_is_parsed) { throw std::logic_error("Nothing parsed, no arguments are available."); }