From d5388dd27dbc1807758f4c79714ec2fff41d1348 Mon Sep 17 00:00:00 2001 From: Tobias Ribizel Date: Fri, 16 Oct 2020 14:41:34 +0200 Subject: [PATCH] Make gko::dim conversion to bool explicit Currently, gko::dim is implicitly convertible to bool. C++ allows for so-called contextual conversions to also use explicit conversion operators. is contextually converted to bool in the following cases: 1. if(), while(), for(...; ; ...) 2. !, ... && , || 3. ? ... : ... 3. static_assert/noexcept() --- include/ginkgo/core/base/dim.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/ginkgo/core/base/dim.hpp b/include/ginkgo/core/base/dim.hpp index c0256df30dc..e0aaf25666c 100644 --- a/include/ginkgo/core/base/dim.hpp +++ b/include/ginkgo/core/base/dim.hpp @@ -118,8 +118,11 @@ struct dim { * different than zero. * * @return true if and only if all dimensions evaluate to true + * + * @note This operator is explicit to avoid implicit dim-to-int casts. + * It will still be used in contextual conversions (if, &&, ||, !) */ - constexpr GKO_ATTRIBUTES operator bool() const + explicit constexpr GKO_ATTRIBUTES operator bool() const { return static_cast(first_) && static_cast(rest_); } @@ -183,7 +186,7 @@ struct dim<1u, DimensionType> { return GKO_ASSERT(dimension == 0), first_; } - constexpr GKO_ATTRIBUTES operator bool() const + explicit constexpr GKO_ATTRIBUTES operator bool() const { return static_cast(first_); }