Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Cumbersome C99 macros in VS2013 math.h - fpclassify =>_CLASSIFY => _CLASS_ARG => warning C6334 and sizeof mess

$
0
0

I'm preparing the jansson json C library for use in a Visual Studio 2013 ATL project and in an effort to make the library "warning free", I stumbled on some strange warnings, as follows:

f:\dev\java\jansson-2.6\src\value.c(773) : warning C6334: sizeof operator applied to an expression with an operator might yield unexpected results: Parentheses can be used to disambiguate certain usages.

It seems the following C99 macros in math.h are the cause of the warnings:

#define isinf(_Val)      (fpclassify(_Val) == FP_INFINITE)
#define isnan(_Val)      (fpclassify(_Val) == FP_NAN)

which further expands to:

#define _CLASS_ARG(_Val)                                  (sizeof ((_Val) + (float)0) == sizeof (float) ? 'f' : sizeof ((_Val) + (double)0) == sizeof (double) ? 'd' : 'l')
#define _CLASSIFY(_Val, _FFunc, _DFunc, _LDFunc)          (_CLASS_ARG(_Val) == 'f' ? _FFunc((float)(_Val)) : _CLASS_ARG(_Val) == 'd' ? _DFunc((double)(_Val)) : _LDFunc((long double)(_Val)))
#define _CLASSIFY2(_Val1, _Val2, _FFunc, _DFunc, _LDFunc) (_CLASS_ARG((_Val1) + (_Val2)) == 'f' ? _FFunc((float)(_Val1), (float)(_Val2)) : _CLASS_ARG((_Val1) + (_Val2)) == 'd' ? _DFunc((double)(_Val1), (double)(_Val2)) : _LDFunc((long double)(_Val1), (long double)(_Val2)))

#define fpclassify(_Val)      (_CLASSIFY(_Val, _fdclass, _dclass, _ldclass))

Generate a preprocessed file of the jansson source file gives a rather complicated series ofsizeof in place of the macros. The original code

    if(isnan(value) || isinf(value))
        return NULL;

turns to

    if(((((sizeof ((value) + (float)0) == sizeof (float) ? 'f' : sizeof ((value) + (double)0) == sizeof (double) ? 'd' : 'l') == 'f' ? _fdclass((float)(value)) : (sizeof ((value) + (float)0) == sizeof (float) ? 'f' : sizeof ((value) + (double)0) == sizeof (double) ? 'd' : 'l') == 'd' ? _dclass((double)(value)) : _ldclass((long double)(value)))) == 2) || ((((sizeof ((value) + (float)0) == sizeof (float) ? 'f' : sizeof ((value) + (double)0) == sizeof (double) ? 'd' : 'l') == 'f' ? _fdclass((float)(value)) : (sizeof ((value) + (float)0) == sizeof (float) ? 'f' : sizeof ((value) + (double)0) == sizeof (double) ? 'd' : 'l') == 'd' ? _dclass((double)(value)) : _ldclass((long double)(value)))) == 1))
        return ((void *)0);

Pretty ugly I must say. Anyway, can I trust the code despite the warnings? Anyone got an idea of an alternative solution?


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>