问题
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