mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 14:44:40 +00:00
Merge pull request #192 from jun-sheaf/patch-2
Use references for any_cast
This commit is contained in:
commit
b8160a86b5
@ -605,11 +605,10 @@ public:
|
||||
if constexpr (!details::IsContainer<T>) {
|
||||
return get<T>() == rhs;
|
||||
} else {
|
||||
using ValueType = typename T::value_type;
|
||||
auto lhs = get<T>();
|
||||
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<const ValueType &>(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 <typename T> T get() const {
|
||||
template <typename T> auto get() const -> std::conditional_t<details::IsContainer<T>, T, const T&> {
|
||||
if (!m_values.empty()) {
|
||||
if constexpr (details::IsContainer<T>) {
|
||||
return any_cast_container<T>(m_values);
|
||||
} else {
|
||||
return std::any_cast<T>(m_values.front());
|
||||
return *std::any_cast<T>(&m_values.front());
|
||||
}
|
||||
}
|
||||
if (m_default_value.has_value()) {
|
||||
return std::any_cast<T>(m_default_value);
|
||||
return *std::any_cast<T>(&m_default_value);
|
||||
}
|
||||
if constexpr (details::IsContainer<T>) {
|
||||
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<ValueType>(value); });
|
||||
[](const auto &value) { return *std::any_cast<ValueType>(&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 <typename T = std::string> T get(std::string_view arg_name) const {
|
||||
template <typename T = std::string> auto get(std::string_view arg_name) const
|
||||
-> std::conditional_t<details::IsContainer<T>, T, const T&> {
|
||||
if (!m_is_parsed) {
|
||||
throw std::logic_error("Nothing parsed, no arguments are available.");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user