if语句中使用显式的static_cast到bool类型

huangapple go评论70阅读模式
英文:

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&lt;bool&gt;(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&lt;bool&gt;.

Are there any reasons to use if (static_cast&lt;bool&gt;(x)) for custom types (structs or classes) convertible to bool?

AFAIK the C++ Core Guidelines do not mention static_cast&lt;bool&gt; 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&lt;bool&gt;(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&lt;bool&gt;(e)) {} // OK

For regular struct/classes, if (static_cast&lt;bool&gt;(x)) is equivalent to if (x), even for types with explicit operator bool, so you can use the later.

答案2

得分: 12

有时候。即使对于只显式转换为 boolclass,在该上下文中也会转换为 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&lt;bool&gt;(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.

huangapple
  • 本文由 发表于 2023年7月10日 23:18:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定