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?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…