Sunday 31 December 2017

c - UNUSED macro warning

itemprop="text">

So I found this href="https://stackoverflow.com/questions/4851075/universally-compiler-independent-way-of-implementing-an-unused-macro-in-c-c/4851173#4851173">macro
on SO:




#define
UNUSED(x) (void)(sizeof((x),
0))


and this (still)
produces the following warning:




main.c:11:36:
warning: left-hand operand of comma expression has no effect [-Wunused-value]

#define UNUSED(x) (void)(sizeof((x),
0))





Whereas
the simpler version, a normal void cast: #define
UNUSED(x) (void)(x)
is
warning-free.



What could be the reason behind
it? In general warnings are a sign of high-risk situations. Is here the given warning
really useful?



I am interested in
C-explanation.


itemprop="text">
class="normal">Answer



This macro
seems inappropriate for your compiler at the current warning
level.



You could use this simpler
version:




#define
UNUSED(x)
(void)(sizeof(x))


x
will not be evaluated either but is used so the compiler should not
complain about x being unused, not about the left hand side of
the , operator being unused in the
expression.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...