From: Mikko Rasa Date: Sun, 31 Oct 2021 13:05:18 +0000 (+0200) Subject: Mark boolean conversion operators as explicit X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=9990be018b49e5aae27218e1b8c6aefdb63b38a7;hp=8e1db641bb244980bc96ee9da4ba2f0dee8274ad Mark boolean conversion operators as explicit Also turn const void * operators into actual boolean conversions. --- diff --git a/source/io/handle.cpp b/source/io/handle.cpp index 693f93f..53b5a7e 100644 --- a/source/io/handle.cpp +++ b/source/io/handle.cpp @@ -25,9 +25,9 @@ Handle::~Handle() delete priv; } -Handle::operator const void *() const +Handle::operator bool() const { - return priv->handle!=INVALID_HANDLE_VALUE ? this : 0; + return priv->handle!=INVALID_HANDLE_VALUE; } diff --git a/source/io/handle.h b/source/io/handle.h index 3e06c71..561d710 100644 --- a/source/io/handle.h +++ b/source/io/handle.h @@ -23,10 +23,7 @@ public: Private &operator*() { return *priv; } const Private &operator*() const { return *priv; } - /** This is effectively a boolean conversion, but avoids accidental - conversion to OS native handles. Unix-based systems use int and win32 uses - void *; const void * is not implicitly convertible to either. */ - operator const void *() const; + explicit operator bool() const; }; diff --git a/source/stringcodec/jisx0208.h b/source/stringcodec/jisx0208.h index ee7b687..3941282 100644 --- a/source/stringcodec/jisx0208.h +++ b/source/stringcodec/jisx0208.h @@ -43,7 +43,7 @@ struct Kuten unsigned short ku = 0; unsigned short ten = 0; - operator bool() { return ku!=0 && ten!=0; } + explicit operator bool() { return ku!=0 && ten!=0; } }; unichar jisx0208_to_ucs(Kuten); diff --git a/source/strings/regmatch.h b/source/strings/regmatch.h index 48f65c6..6af309e 100644 --- a/source/strings/regmatch.h +++ b/source/strings/regmatch.h @@ -32,7 +32,7 @@ public: size_type length; //< Length of the match (end-begin) std::string str; //< The part of the string that matched - operator bool() const { return match; } + explicit operator bool() const { return match; } }; private: @@ -65,7 +65,7 @@ public: /** Shorthand for the group() function. */ const Group &operator[](unsigned i) const { return group(i); } - operator bool() const { return !empty(); } + explicit operator bool() const { return !empty(); } }; } // namespace Msp diff --git a/source/time/timedelta.h b/source/time/timedelta.h index a3e62f1..1a4274b 100644 --- a/source/time/timedelta.h +++ b/source/time/timedelta.h @@ -54,7 +54,7 @@ public: bool operator==(const TimeDelta &t) const { return usec==t.usec; } bool operator!=(const TimeDelta &t) const { return usec!=t.usec; } - operator const void *() const { return usec ? this : 0; } + explicit operator bool() const { return usec; } }; template diff --git a/source/time/timestamp.h b/source/time/timestamp.h index 2a5c4e1..8068510 100644 --- a/source/time/timestamp.h +++ b/source/time/timestamp.h @@ -46,7 +46,7 @@ public: bool operator==(const TimeStamp &t) const { return usec==t.usec; } bool operator!=(const TimeStamp &t) const { return usec!=t.usec; } - operator const void *() const { return usec>0 ? this : 0; } + explicit operator bool() const { return usec>0; } static TimeStamp from_unixtime(time_t t) { return TimeStamp(t*1000000LL); } };