Is there any advantage in using static_cast rather than C-style casting for non-pointer types?

后端 未结 2 1006
南旧
南旧 2021-02-11 15:10

I am well aware of the advantage in using static_cast rather than C-style casting for pointer types.

If the pointer types are incompatible, then:

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-11 15:57

    One advantage which the other two answers didn't mention yet is that static_cast is much easier to spot. The meaning of parentheses is notoriously overloaded in C++ and it can be difficult to spot evil (or even incorrect) casts. When I see something ending in _cast though, it's like a mental speed bump: I slow down and carefully check why the type system is being subverted.

提交回复
热议问题