11

The title is quite clear, is there a way to disable every single non-standard feature of gcc (extension) when compiling some C++ code. I've previously always used -pedantic-errors alongside -Wall and -Wextra, the first according to the gcc man page does the following

Give an error whenever the base standard (see -Wpedantic) requires a diagnostic, in some equivalent to -Werror=pedantic, since there are errors enabled by this option and not particular library's limitations. However, if -Wpedantic is used with -Wformat, warnings made into an error by -pedantic-errors. -Wall or -Wpedantic.

As a result it disables practically all extensions that aren't standard conforming, however. I was rather shooked that the following code compiles with gcc

void foo(std::vector<auto>) { ... }

with the -std=c++20 flag, if we bump it down to -std=c++17 we receive an error message prompting the use of -std=c++20 as it's a "c++20 feature". However neither clang or msvc is willing to compile this. After some further research I was notified of the following

This is a GCC extension and accepted by design.

Which is rather upsetting as even the safe-guards of -pedantic-errors, -Wall and -Wextra didn't pick this up, which leads me to question what other extensions might be silently passed through by gcc. Thus the question comes; is there any way to disable exactly every gcc extension?

Edit: From the comment section of How to disable GNU C extensions?

You cannot

Noak Palander
  • 111
  • 1
  • 4
  • Does this answer your question? [How to disable GNU C extensions?](https://stackoverflow.com/questions/38939991/how-to-disable-gnu-c-extensions) – ChrisMM May 18 '22 at 00:13
  • TL;DR of duplicate ... You cannot. – ChrisMM May 18 '22 at 00:13
  • 5
    If you want guarantees you're out of luck. All modern compilers (I've ever used) allow some non-standard features to sneak past, even with concerted effort (multiple command line options, etc) to disable them. Where I care about such things, I build with multiple different/dissimilar compilers and code analysers (and sometimes versions), each tweaked to its "most picky" mode. That increases chances of, but does not guarantee, catching dubious coding including use of non-standard features - human review has its place, no matter how much folks want toolsets to do all the heavy lifting. – Peter May 18 '22 at 02:10
  • Are you sure it isn't a c++20 feature and the other compilers just haven't gotten around to implementing it? Usually GCC extensions require `-std=gnu++20`. – Goswin von Brederlow May 18 '22 at 08:30
  • Yes I am, it's not anything standard. – Noak Palander May 18 '22 at 10:16

0 Answers0