25% of programmers work time is spended by checking if the required code already exist.
I'm searching for a base class for implementing the safe bool idiom.
25% of programmers work time is spended by checking if the required code already exist.
I'm searching for a base class for implementing the safe bool idiom.
bool_testable<>
in Boost.Operators looks promising.
The reference mentions that:
bool_testable
provides the antithesis ofoperator bool
, such that the expressionif (!p)
is valid, whilst also makingoperator bool
safer by preventing accidental conversions to integer types. ...bool_testable<>
prevents these accidental conversions by declaring a private conversion operator to signed char, and not defining the body.
bool_testable<>
was removed from the library, apparently without replacement. –
Adolphadolphe The safest thing to do is to not have implicit conversion to bool
.
Make that conversion explicit.
Then the name, if chosen well, can also help people understand what it's all about.
Cheers & hth.,
bool convert_to_bool();
. Or, to follow the last part of Alf's advance, a function with a better name that indicates what the result actually means. –
Dentilabial bool_type
typedef from another type (usually a member pointer) and providing an operator bool_type
. –
Verdie © 2022 - 2024 — McMap. All rights reserved.