Skip to content

Commit

Permalink
Make gko::dim conversion to bool explicit
Browse files Browse the repository at this point in the history
Currently, gko::dim is implicitly convertible to bool.
C++ allows for so-called contextual conversions to also
use explicit conversion operators.
<expression> is contextually converted to bool in the following cases:

1. if(<expression>), while(<expression>), for(...; <expression>; ...)
2. !<expression>, ... && <expression>, || <expression>
3. <expression> ? ... : ...
3. static_assert/noexcept(<expression>)
  • Loading branch information
upsj committed Oct 16, 2020
1 parent a977659 commit 13a8eba
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions include/ginkgo/core/base/dim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>(first_) && static_cast<bool>(rest_);
}
Expand Down Expand Up @@ -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<bool>(first_);
}
Expand Down

0 comments on commit 13a8eba

Please sign in to comment.