Is it possible to use GNU extensions unless pedantic?

118 views Asked by At

Is there a way to conditionally enable features that rely on GCC extensions (such as __int128) if they would compile?

I want to do something like

void foo(int);
void foo(long);
void foo(long long);
#if not_pedantic
void foo(__int128);
#endif

I could not find a macro that is defined when -pedantic or pedantic-error is passed.

2

There are 2 answers

0
HolyBlackCat On BEST ANSWER

This is a bad idea. Only check the compiler, and disable the -pedantic-errors warnings with #pragmas or __extension__:

void foo(int);
void foo(long);
void foo(long long);
#ifdef __GNUC__
__extension__ void foo(__int128);
#endif

If your user builds with -pedantic-errors but still uses the extensions as needed, they would be very annoyed if you disable overloads like this.

0
Yksisarvinen On

gcc offers __STRICT_ANSI__ that is defined if you have set -ansi or -std=c++XX (as opposed to -std=gnu++XX which enables extensions). This does not check for -pedantic, only for the flags that were mentioned:

void foo(int);
void foo(long);
void foo(long long);
#ifndef __STRICT_ANSI__
void foo(__int128);
#endif

I don't see any way to check for -pedantic specifically. There doesn't seem to be any macro or pragma that allows checking (not setting) specific compiler flags.