我的班级明确转换为bool:
struct T {
explicit operator bool() const { return true; }
};
Run Code Online (Sandbox Code Playgroud)
我有一个例子:
T t;
Run Code Online (Sandbox Code Playgroud)
要将它分配给类型的变量bool,我需要编写一个强制转换:
bool b = static_cast<bool>(t);
bool b = bool(t);
bool b(t); // converting initialiser
bool b{static_cast<bool>(t)};
Run Code Online (Sandbox Code Playgroud)
我知道我可以在没有强制转换的条件下直接使用我的类型,尽管有explicit限定符:
if (t)
/* statement */;
Run Code Online (Sandbox Code Playgroud)
我还可以t在bool没有演员阵容的情况下使用?