4

I don't understand the interest of the g++ -pedantic flag.

Code isn't supposed to abide by the C++ standards to compile by default ?

If I add this flag or if I don't add it, the compiler should logically issue the same warnings right ?

Tesla123
  • 319
  • 1
  • 7
  • 1
    G++ is not unique in defaulting to turning on extensions that are not actually part of the standard. Sometimes it causes confusion: there's a whole lot of questions that end up boiling down to [this question](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) because G++ turns on variable-length arrays by default. But it's nevertheless the case. – Nathan Pierson Feb 20 '23 at 17:40
  • 2
    Two words: Backwards compatibility. More words: There were C++ compilers before C++ was standardized so there are C++ code bases that have pre-standard code, and a lot of those were allowed to keep compiling. – NathanOliver Feb 20 '23 at 17:41
  • Two more words: Commercial Expediency. If I used a compiler that ate my cat every time I wrote an undefined statement like `unsigned n = -1e1;` I'd switch to another vendor. – Bathsheba Feb 20 '23 at 19:54

2 Answers2

5

If I add this flag or if I don't add it, the compiler should logically issue the same warnings right ?

No, compilers typically default to a variation of C++ with extensions and minor differences relative to the standard. For example GCC by default supports variable-length arrays. According to the standard these are ill-formed and so GCC should produce a diagnostic for them. -pedantic does that.

To get closest to standard conformance you need -std=c++XX -pedantic-errors on GCC with XX replaced by the standard version. -pedantic-errors can also be -pedantic. Warnings for ill-formed code are sufficient for standard-conformance.

100% conformance isn't possible anyway. The language specification is too complex and not all details are well-specified. In some cases compilers also intentionally deviate from the specification even in stricter conformance mode because it makes more sense in practice to consider the specified behavior a defect in the standard, even if it hasn't been resolved as such yet.

If C++ already needs to abide by C++ standards to compile [...]

The question doesn't really make sense. Where would the imperative that code needs to abide by the standard come from in a non-circular way? Of course a compiler can also support code that isn't well-formed according to the standard. There is no higher authority requiring that a "C++ compiler" must conform to the ISO C++ standard.

And even if the compiler intents that to be the case: In some cases the standard says that a conforming compiler should issue some diagnostic to be conforming, but it never requires a compiler not to support any given code as valid.

user17732522
  • 53,019
  • 2
  • 56
  • 105
1

The point of pedantic is explained in the documentation:

Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++.

There are code constructs, in which the warning is not demanded by ISO C++ and the behavior of a program is undefined according to ISO C++. But undefined behavior means that the standard does not place any requirements on the behavior of the program. So the compiler can come up with its own behavior. Such behavior is standard conforming, "abides" by C++ standard, but still is something that is not inside C++ standard. -pedantic is to warn on programs that use such extensions to the standard.

-pedantic is for https://eel.is/c++draft/intro#compliance.general-8 :

A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program. Implementations are required to diagnose programs that use such extensions that are ill-formed according to this document. Having done so, however, they can compile and execute such programs.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111