Your class defines an explicit conversion to bool, enabling you to use its instance 't' directly in conditional statements. However, this explicit conversion poses the question: where else can 't' be used as a bool without a cast?
Contextual Conversion Scenarios
The C++ standard specifies four main scenarios where a value can be contextually converted to bool:
Statements:
if, while, for, do-while statements
if (t)
/* statement */;
Expressions:
Negation (!), logical AND (&&), logical OR (||), ternary operator (?)
!t
t && t2
Compile-Time Tests:
static_assert, noexcept, explicit, if constexpr (requires constexpr conversion operator)
static_assert(t);
noexcept(t)
Algorithms and Concepts:
NullablePointer, predicate or comparator arguments in STL algorithms
NullablePointer T
std::remove_if(first, last, [&](auto){ return t; });
Additional Notes:
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3