简体   繁体   中英

C++ cast to void

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 ?

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM