I have a macro defined as follows:
#define UNREF_PARAM_1(a)
do { \
(void)sizeof(a); \
} \
while (0)
To get rid of compiler warnings. In a new project I am working on, VS2013 is suddenly complaining again about a unreferenced formal parameter.
Strangely it does work if I just use (void)param
.
Does anybody have a clue why it does not work when using it with (void)sizeof(param)
?
Because in
sizeof(param)
,param
is a so-called unevaluated operand and therefore not odr-used - that is, not needed at runtime.However,
(void)param
does constitute a odr-use.A cast with the notation in your code internally invokes a
static_cast
. [expr.static.cast]/6:[expr]/10:
[basic.def.odr]/2:
The first part of this quote specifies that
sizeof(a)
isn't an odr-use ofa
sincea
is an unevaluated operand1.Clearly
(void)a
is potentially evaluated. And becausea
is certainly neither allowed to appear in a constant expression nor declaredvolatile
, no lvalue-to-rvalue conversion is "immediately applied" and thusa
is odr-used.1) Here is a list of expressions where
x
is an unevaluated operand as from C++11:typeid(x)
, wherex
is not a glvalue of polymorphic class typesizeof(x)
(andsizeof x
)noexcept(x)
decltype(x)
alignof(x)
?alignas(x)
?