From ca68260ec4eccb34ffc02e51d61ced1f070ed052 Mon Sep 17 00:00:00 2001 From: Stephan van Veen Date: Mon, 13 May 2019 21:50:53 +0200 Subject: [PATCH] Enable equality operator for all iterable types --- include/argparse.hpp | 55 ++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 2cc9e69..06bacd3 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -52,6 +52,31 @@ struct is_specialization : std::false_type {}; template class Ref, typename... Args> struct is_specialization, Ref> : std::true_type {}; +template +struct is_container_helper {}; + +template +struct is_container : std::false_type {}; + +template +struct is_container().begin()), + decltype(std::declval().end()), + decltype(std::declval().size()) + >, void>> : public std::true_type { +}; + +template +static constexpr bool is_container_v = is_container::value; + +template +using enable_if_container = std::enable_if_t, T>; + +template +using enable_if_not_container = std::enable_if_t, T>; + // Check if string (haystack) starts with a substring (needle) bool starts_with(const std::string& haystack, const std::string& needle) { return needle.length() <= haystack.length() @@ -129,38 +154,22 @@ public: // Entry point for template types other than std::vector and std::list template - typename std::enable_if::value && - !is_specialization::value, bool>::type - operator==(const T& aRhs) const { + std::enable_if_t , bool> + operator==(const T& aRhs) const { return get() == aRhs; } - // Template specialization for std::vector<...> + // Template specialization for containers template - typename std::enable_if::value, bool>::type + std::enable_if_t , bool> operator==(const T& aRhs) const { using ValueType = typename T::value_type; - T tLhs = get(); + auto tLhs = get(); if (tLhs.size() != aRhs.size()) return false; else { - return std::equal(std::begin(tLhs), std::begin(tLhs), std::begin(aRhs), [](const auto& lhs, const auto& rhs) { - return std::any_cast(lhs) == rhs; - }); - } - } - - // Template specialization for std::list<...> - template - typename std::enable_if::value, bool>::type - operator==(const T& aRhs) const { - using ValueType = typename T::value_type; - T tLhs = get(); - if (tLhs.size() != aRhs.size()) - return false; - else { - return std::equal(std::begin(tLhs), std::begin(tLhs), std::begin(aRhs), [](const auto& lhs, const auto& rhs) { - return std::any_cast(lhs) == rhs; + return std::equal(std::begin(tLhs), std::end(tLhs), std::begin(aRhs), [](const auto& lhs, const auto& rhs) { + return std::any_cast(lhs) == rhs; }); } }