Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it not safe to turn off the __STRICT_ANSI__ flag?

Tags:

c++

c

c++11

I am creating an application which uses the Apple GLKit headers. In this library, they turn off unions if __STRICT_ANSI__ is defined. I know how to undefined this via compiler flag, but I hate to do this without knowing why they had this in there, and under what circumstances this matters?

Code From Apple's source:

#if defined(__STRICT_ANSI__)
struct _GLKVector4
{
    float v[4];
} __attribute__((aligned(16)));
typedef struct _GLKVector4 GLKVector4;  
#else
union _GLKVector4
{
    struct { float x, y, z, w; };
    struct { float r, g, b, a; };
    struct { float s, t, p, q; };
    float v[4];
} __attribute__((aligned(16)));
typedef union _GLKVector4 GLKVector4;
#endif
like image 299
David Avatar asked Oct 16 '25 21:10

David


1 Answers

This kind of protections are used on code that is using non standard constructs or specific to some development tools. The same thing is present in Microsoft headers for exactly the same issue: nameless structures/unions.

In your specific case the definition uses nameless structures that where not allowed before C11 standard, where a standard macro __STDC_VERSION__ is defined with value 201112L to indicate that C11 support is available.

This seems just a precaution for old compilers. If you try to compile with a pre-C11 you'll get an error.

The correct definition should be:

#if defined(__STDC_VERSION__) && (__STDC_VERSION__ < 201112L)

Note the comparison for __STDC_VERSION__ less than 201112L in the supposition that newer standard will keep support for nameless structures/unions.

Anyway some care must always be taken before removing conditional compilations when we haven't correctly identified the reason for which they are there.

like image 199
Frankie_C Avatar answered Oct 18 '25 09:10

Frankie_C