Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is a stray semicolon no longer detected by `-pedantic` modern compilers?

The following snippet generates compilation errors on adding -pedantic and -Werror on compilers that are a bit old.

#include <cstdint>
#include <iostream>

int add(int a, int b){
    return a + b;
}; // <-- stray semicolon

int main (){
    return 0;
}

However this does not happen newer compiler versions. Please find a matrix of gcc (10.x, 11.x) and clang (5.x, 6.x) demonstrating the difference at https://godbolt.org/z/KWeb8WTxz.

I have two parts to my question:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Why is this not triggered in recent compilers?
  2. Is it possible to enable the old behaviour in recent versions of clang or gcc?

>Solution :

Starting in C++11, extra semicolons ; (aka empty-declarations) at the global level are valid. I believe this is occasionally useful for writing macros.

As such, GCC 11 removed -pedantic diagnostics for an extra ; when -std=c++11 or later is used. See:

You can restore the old behavior by using a C++ standard older than C++11. Both GCC 11 and clang 6 will emit the old diagnostics if you pass -std=c++03.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading