【发布时间】:2017-02-21 01:50:30
【问题描述】:
我的班级有一个明确的布尔转换:
struct T {
explicit operator bool() const { return true; }
};
我有它的一个实例:
T t;
要将其分配给bool 类型的变量,我需要编写一个演员表:
bool b = static_cast<bool>(t);
bool b = bool(t);
bool b(t); // converting initialiser
bool b{static_cast<bool>(t)};
我知道我可以在没有强制转换的情况下直接在条件中使用我的类型,尽管有 explicit 限定符:
if (t)
/* statement */;
我还可以在哪里使用 t 作为 bool 而无需演员?
【问题讨论】: