Unverified Commit 394b17c0 authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #640 from muttleyxd/add-comparison-operators

optional: add equal_to and not_equal_to operators
parents 97d0cb3b 7bc34bde
......@@ -14,6 +14,7 @@
#include <iostream>
#include <tuple>
#include <functional>
#include <type_traits>
namespace Pistache {
......@@ -81,6 +82,17 @@ namespace types {
struct is_move_constructible :
std::is_constructible<T, typename std::add_rvalue_reference<T>::type> {};
// Workaround for C++14 defect (CWG 1558)
template <typename... Ts> struct make_void { typedef void type; };
template <typename... Ts> using void_t = typename make_void<Ts...>::type;
template <typename, typename = void_t<>>
struct has_equalto_operator : std::false_type {};
template <typename T>
struct has_equalto_operator<
T, void_t<decltype(std::declval<T>() == std::declval<T>())>>
: std::true_type {};
}
......@@ -228,6 +240,15 @@ public:
}
}
template<typename U = T, typename = typename std::enable_if<types::has_equalto_operator<U>::value>::type>
bool operator==(const Optional<T>& other) const {
return (isEmpty() && other.isEmpty()) || (!isEmpty() && !other.isEmpty() && get() == other.get());
}
template<typename U = T, typename = typename std::enable_if<types::has_equalto_operator<U>::value>::type>
bool operator!=(const Optional<T>& other) const {
return !(*this == other);
}
private:
T *constData() const {
......
File mode changed from 100755 to 100644
......@@ -125,3 +125,49 @@ TEST(optional, integer_none)
Optional<int32_t> value(Pistache::None());
EXPECT_TRUE(value.isEmpty());
}
TEST(optional, equal_operator_empty_equalto_empty)
{
Optional<int32_t> value(Pistache::None());
Optional<int32_t> value2(Pistache::None());
EXPECT_EQ(value, value2);
}
TEST(optional, equal_operator_value_equalto_value)
{
Optional<int32_t> value(Pistache::Some(1));
Optional<int32_t> value2(Pistache::Some(1));
EXPECT_EQ(value, value2);
}
TEST(optional, equal_operator_empty_notequalto_value)
{
Optional<int32_t> value(Pistache::None());
Optional<int32_t> value2(Pistache::Some(2));
EXPECT_NE(value, value2);
}
TEST(optional, equal_operator_value_notequalto_value)
{
Optional<int32_t> value(Pistache::Some(1));
Optional<int32_t> value2(Pistache::Some(2));
EXPECT_NE(value, value2);
}
struct not_comparable
{
bool operator==(const not_comparable& other) const = delete;
};
TEST(optional, is_comparable_type)
{
using Pistache::types::has_equalto_operator;
EXPECT_FALSE(has_equalto_operator<not_comparable>::value);
EXPECT_TRUE(has_equalto_operator<int>::value);
EXPECT_TRUE(has_equalto_operator<double>::value);
EXPECT_TRUE(has_equalto_operator<std::string>::value);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment