My class has an explicit conversion to bool:
struct T {
explicit operator bool() const { return true; }
};
and I have an instance of it:
T t;
To assign it to a variable of type bool
, I need to write a cast:
bool b = static_cast<bool>(t);
bool b = bool(t);
bool b(t); // converting initialiser
bool b{static_cast<bool>(t)};
I know that I can use my type directly in a conditional without a cast, despite the explicit
qualifier:
if (t)
/* statement */;
Where else can I use t
as a bool
without a cast?
Explicit conversion operators are those that do require an explicit cast. As an example, the following code shows a simple console application to covert a weight in Pounds to Kilograms.
"What is a Boolean Operator?" Boolean Operators are simple words (AND, OR, NOT or AND NOT) used as conjunctions to combine or exclude keywords in a search, resulting in more focused and productive results. This should save time and effort by eliminating inappropriate hits that must be scanned before discarding.
The standard mentions places where a value may be "contextually converted to bool
". They fall into four main groups:
if (t) /* statement */;
for (;t;) /* statement */;
while (t) /* statement */;
do { /* block */ } while (t);
!t
t && t2
t || t2
t ? "true" : "false"
static_assert(t);
noexcept(t)
explicit(t)
if constexpr (t)
The conversion operator needs to be constexpr
for these.
NullablePointer T
Anywhere the Standard requires a type satisfying this concept (e.g. the pointer
type of a std::unique_ptr
), it may be contextually converted. Also, the return value of a NullablePointer
's equality and inequality operators must be contextually convertible to bool
.
std::remove_if(first, last, [&](auto){ return t; });
In any algorithm with a template parameter called Predicate
or BinaryPredicate
, the predicate argument can return a T
.
std::sort(first, last, [&](auto){ return t; });
In any algorithm with a template parameter called Compare
, the comparator argument can return a T
.
(source1, source2)
Do be aware that a mix of const and non-const conversion operators can cause confusion:
explicit operator bool
not in effect as expected?If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With