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),paramis a so-called unevaluated operand and therefore not odr-used - that is, not needed at runtime.However,
(void)paramdoes 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 ofasinceais an unevaluated operand1.Clearly
(void)ais potentially evaluated. And becauseais certainly neither allowed to appear in a constant expression nor declaredvolatile, no lvalue-to-rvalue conversion is "immediately applied" and thusais odr-used.1) Here is a list of expressions where
xis an unevaluated operand as from C++11:typeid(x), wherexis not a glvalue of polymorphic class typesizeof(x)(andsizeof x)noexcept(x)decltype(x)alignof(x)?alignas(x)?