What is the difference between static_cast and Implicit_cast?

后端 未结 4 616
小蘑菇
小蘑菇 2020-12-05 05:49

What is implicit_cast? when should I prefer implicit_cast rather than static_cast?

4条回答
  •  忘掉有多难
    2020-12-05 06:21

    Implicit conversions, explicit conversions and static_cast are all different things. however, if you can convert implicitly, you can convert explicitly, and if you can convert explicitly, you can cast statically. The same in the other direction is not true, however. There is a perfectly reasonable relationship between implicit casts and static casts. The former is a subset of the the latter.

    See section 5.2.9.3 of the C++ Standard for details

    Otherwise, an expression e can be explicitly converted to a type T using a static_cast of the form static_- cast(e) if the declaration T t(e); is well-formed, for some invented temporary variable t (8.5).

    C++ encourages use of static_casts because it makes the conversion 'visible' in the program. Usage of casts itself indicates some programmer enforced rule which is worth a look so better use static_cast.

提交回复
热议问题