C++ cast to void

一曲冷凌霜 提交于 2019-12-24 12:35:00

问题


As I understand, C++ standard says that casting to void is correct only in case of function-style casting (ISO/IEC 14882:2003, 5.2.3).

But I can't find anything about C-style casting to void in C++ standard.

Is behavior of program only implementation-defined in this case ?


回答1:


As I understand, C++ standard says that casting to void is correct only in case of function-style casting

No, it can be done by static_cast, and therefore also by conversions using functional or cast notation.

But I can't find anything about C-style casting to void in C++ standard.

It's defined for static_cast by [expr.static.cast], 5.2.9/6:

Any expression can be explicitly converted to type cv void, in which case it becomes a discarded-value expression.

[expr.cast], 5.4, describes how a C-style cast can use static_cast, so it's also valid for that style. [expr.type.conv], 5.2.3, described how functional style is equivalent to C-style, so it's also valid for that style.

(Note: section numbers refer to C++11 (ISO/IEC 14882:2011), not C++03 which you refer to, since that's the current version. Other versions may differ, but probably not much.)



来源:https://stackoverflow.com/questions/26115775/c-cast-to-void

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!