From 1550529a39137532051c1df1124cc7ceb896f1e5 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 21 Jul 2020 15:05:21 +1000 Subject: [PATCH] types/tagged: add an equality operator --- types/tagged.hpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/types/tagged.hpp b/types/tagged.hpp index 9bf7b9fb..f85a19f6 100644 --- a/types/tagged.hpp +++ b/types/tagged.hpp @@ -201,4 +201,24 @@ namespace cruft { arg ); } + + + template + bool operator== (tagged const &lhs, tagged const &rhs) + { + // Check the tags actually match to start with. This lets us use + // short-circuiting in a second for the actual equality check. + if (lhs.tag () != rhs.tag ()) + return false; + + // Construct a fold-expression that tests every component type for + // equality. Use short-circuiting with `is` to protect against `get` + // queries for the wrong types. + return ( + ( + lhs.template is () && + (lhs.template get () == rhs.template get ()) + ) || ... + ); + } }