英文:
if statement with explicit static_cast to bool
问题
是否对于某些类型的x,使用if (static_cast<bool>(x))有意义?
显然,如果x是一个bool型,就不需要使用static_cast。
如果x是一个原始指针、std::unique_ptr或std::shared_ptr,那么if (x)是可以的(特别是因为std::unique_ptr和std::shared_ptr都有一个bool运算符)。也许if (x != nullptr)可能更好。
如果x是一个int,我认为最好的方式是if (x != 0)。
如果x是一个double,我认为最好的方式是if (x != 0.0)(尽管检查精确的浮点值有其自己的问题)。
如果x是某个枚举类型,if (x != E())或者if (x != E::whatever)可能比static_cast<bool>更好。
是否有任何理由在可转换为bool的自定义类型(结构体或类)上使用if (static_cast<bool>(x))?
据我所知,C++核心指南没有明确提到static_cast<bool>。我知道以下讨论:
https://stackoverflow.com/questions/58674726/if-static-castboolx-vs-if-x(仅涵盖指针)和
https://stackoverflow.com/questions/14242218/int-to-bool-casting。
英文:
Does if (static_cast<bool>(x)) make sense for some type of x?
Obviously if x is a bool, static_cast is not required.
If x is a raw pointer or std::unique_ptr or std::shared_ptr, then if (x) is alright (especially since std::unique_ptr and std::shared_ptr both have a bool operator). Maybe if (x != nullptr) might be better.
If x is an int, I guess if (x != 0) is preferred.
If x is a double, I guess if (x != 0.0) is preferred (although checking for exact floating point values has its own issues).
If x is some enum, if (x != E()) or if (x != E::whatever) probably is better than static_cast<bool>.
Are there any reasons to use if (static_cast<bool>(x)) for custom types (structs or classes) convertible to bool?
AFAIK the C++ Core Guidelines do not mention static_cast<bool> explicitly. I'm aware of following discussions:
https://stackoverflow.com/questions/58674726/if-static-castboolx-vs-if-x (which covers pointers only) and
https://stackoverflow.com/questions/14242218/int-to-bool-casting
答案1
得分: 18
对于某些类型的x,
if (static_cast<bool>(x))有意义吗?
对于枚举类(enum class),是的:
enum class E {
A = 1
};
E e = E::A;
if (e) {} // 不正确
if (static_cast<bool>(e)) {} // 正确
对于常规的结构体/类(struct/classes),if (static_cast<bool>(x)) 等同于 if (x),即使对于具有 explicit operator bool 的类型也是如此,因此您可以使用后者。
英文:
> Does if (static_cast<bool>(x)) make sense for some type of x?
Yes for enum class:
enum class E {
A = 1
};
E e = E::A;
if (e) {} // KO
if (static_cast<bool>(e)) {} // OK
For regular struct/classes, if (static_cast<bool>(x)) is equivalent to if (x), even for types with explicit operator bool, so you can use the later.
答案2
得分: 12
有时候。即使对于只显式转换为 bool 的 class,在该上下文中也会转换为 bool。
struct Foo {
explicit operator bool() const { return true; }
};
int main() {
Foo f;
if (f) {} // 编译通过
}
然而,如果你有一个具有多个用户定义的转换的类...
struct Foo {
explicit operator bool() const { return true; }
operator int() const { return 0; } // 风险较大的非显式转换
};
...你可能希望明确要使用哪一个。
英文:
> Are there any reasons to use if (static_cast<bool>(x)) for custom types (structs or classes) convertible to bool?
Not often. Even classes only explicitly convertible to bool will convert to bool in that context.
struct Foo {
explicit operator bool() const { return true; }
};
int main() {
Foo f;
if(f) {} // compiles fine
}
If you however have a class with multiple user-defined conversions ...
struct Foo {
explicit operator bool() const { return true; }
operator int() const { return 0; } // risky non-explicit
};
... you may want to clarify exactly which one you aim to use.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论