From 36b6dc1ff6be4e5dcdd617c1d87b3b78b0696b46 Mon Sep 17 00:00:00 2001 From: Simon O'Dwyer Date: Fri, 15 Sep 2017 16:11:41 +0200 Subject: [PATCH] allow tagged_bool to be used in conditional expressions with literal bools eg. if( myTaggedBool == true ) --- include/ak_toolkit/tagged_bool.hpp | 5 +++++ test/test_explicit.cpp | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/ak_toolkit/tagged_bool.hpp b/include/ak_toolkit/tagged_bool.hpp index b591b23..783954c 100644 --- a/include/ak_toolkit/tagged_bool.hpp +++ b/include/ak_toolkit/tagged_bool.hpp @@ -34,7 +34,12 @@ class tagged_bool constexpr tagged_bool operator!() const { return tagged_bool{!value}; } friend constexpr bool operator==(tagged_bool l, tagged_bool r) { return l.value == r.value; } + friend constexpr bool operator==(tagged_bool l, bool r) { return l.value == r; } + friend constexpr bool operator==(bool l, tagged_bool r) { return l == r.value; } + friend constexpr bool operator!=(tagged_bool l, tagged_bool r) { return l.value != r.value; } + friend constexpr bool operator!=(tagged_bool l, bool r) { return l.value != r; } + friend constexpr bool operator!=(bool l, tagged_bool r) { return l != r.value; } }; } diff --git a/test/test_explicit.cpp b/test/test_explicit.cpp index e593e56..66a60f6 100644 --- a/test/test_explicit.cpp +++ b/test/test_explicit.cpp @@ -173,6 +173,18 @@ void demonstrate_tagged_bool() if (a) assert (true); else assert (false); + if (a == true) assert(true); + else assert(false); + + if (true == a) assert(true); + else assert(false); + + if (a != false) assert(true); + else assert(false); + + if (false != a) assert(true); + else assert(false); + constexpr BoolB ba {a}; assert (ba); static_assert (ba && a, "failed tagged_bool");