What exactly is or was the purpose of C++ function-style casts?

后端 未结 6 1929
Happy的楠姐
Happy的楠姐 2020-11-30 07:17

I am talking about \"type(value)\"-style casts. The books I have read pass over them quickly, saying only that they are semantically equivalent to C-style casts, \"(type) va

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 08:05

    It is somewhat easier to parse the function-style casts in a language where parentheses are already heavily (over-)used.

    Were they a mistake? Possibly - but only to the extent that they were unable to completely supersede the C-style casts, and therefore provided a Perl-like "there's more than one way to do it" mechanism for casting. With the explicit and distinctive modern casts:

    dynamic_cast(value);
    reinterpret_cast(value);
    static_cast(value);
    const_cast(value);
    

    there is no reason to use the C-style casts any more, and less reason to use function-style casts.

提交回复
热议问题