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

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:

  • [GCC Bug 96068] Extra semicolon outside of a function should be allowed after c++11?.
  • [GCC Bugs Mailing list] -Wpedantic doesn’t warn about extra semicolons anymore
  • [CWG 569] Spurious semicolons at namespace scope should be allowed

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.

Alternatively, recent versions of both GCC and Clang support the warning option -Wextra-semi which specifically warns about redundant semicolons. Thanks to HolyBlackCat for mentioning this.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)