From 066d00d1172ea5dee467dcd60f2d070d6148e5d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20G=C3=B6rner?= Date: Tue, 15 May 2018 22:56:22 +0200 Subject: [PATCH] Add const and constexpr specifiers to Key union Making the member functions of Key `const' explicitly flags that they will not change the union. This will allow to use Key in const contexts. Adding the `constexpr' specifier to the function makes it possible to rely on the results at compile time. This puts some kind of restrictions on the function, especially when using C++11 and not a newer standard, but these restrictions were already fulfilled, so this seems to be safe. --- src/key_defs.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/key_defs.h b/src/key_defs.h index 5e02389f..76026368 100644 --- a/src/key_defs.h +++ b/src/key_defs.h @@ -26,41 +26,41 @@ union Key { }; uint16_t raw; - inline bool operator==(uint16_t rhs) { + constexpr inline bool operator==(uint16_t rhs) const { return this->raw == rhs; } - inline bool operator==(const Key rhs) { + constexpr inline bool operator==(const Key rhs) const { return this->raw == rhs.raw; } inline Key& operator=(uint16_t raw) { this->raw = raw; return *this; } - inline bool operator!=(const Key& rhs) { + constexpr inline bool operator!=(const Key& rhs) const { return !(*this == rhs); } - inline bool operator>=(uint16_t raw) { + constexpr inline bool operator>=(uint16_t raw) const { return this->raw >= raw; } - inline bool operator<=(uint16_t raw) { + constexpr inline bool operator<=(uint16_t raw) const { return this->raw <= raw; } - inline bool operator>(uint16_t raw) { + constexpr inline bool operator>(uint16_t raw) const { return this->raw > raw; } - inline bool operator<(uint16_t raw) { + constexpr inline bool operator<(uint16_t raw) const { return this->raw < raw; } - inline bool operator>=(const Key& other) { + constexpr inline bool operator>=(const Key& other) const { return this->raw >= other.raw; } - inline bool operator<=(const Key& other) { + constexpr inline bool operator<=(const Key& other) const { return this->raw <= other.raw; } - inline bool operator>(const Key& other) { + constexpr inline bool operator>(const Key& other) const { return this->raw > other.raw; } - inline bool operator<(const Key& other) { + constexpr inline bool operator<(const Key& other) const { return this->raw < other.raw; } };