What C++ standard does GCC 16 compile by default, and how does -std=gnu++20 differ from -std=c++20?
answer
- the default moved in 16
- gnu means extensions on
- __STRICT_ANSI__ marks strict mode
- strict is not pedantic
- pin -std in the build
basics
~20 sGCC 16 defaults to -std=gnu++20, up from gnu++17 in GCC 11 to 15. The gnu++ dialects keep GNU extensions that conflict with ISO C++; -std=c++20 disables those, but other extensions remain until -Wpedantic diagnoses them.
solid answer
~40 sWithout a `-std` option, **GCC 16 compiles C++ as `-std=gnu++20`**; GCC 11 to 15 used `gnu++17` and GCC 6 to 10 `gnu++14`, so an unpinned build changes language on a compiler upgrade. A `gnu++NN` dialect is the ISO standard plus GNU extensions that occasionally conflict with it. `-std=c++20` asks for strict conformance: GCC defines `__STRICT_ANSI__`, stops treating `typeof` as a keyword, and drops predefined macros outside the reserved namespace such as `linux` and `unix`. Strict mode is not pedantic mode, though: extensions that do not conflict, such as variable-length arrays, still compile until `-Wpedantic` or `-pedantic-errors` is added. In GCC 16 the `c++23` and `c++26` dialects are documented as experimental, and C++20 modules need `-fmodules`.
code
cpp · 10 lines#include <cstdio>
int main() {
#ifdef __STRICT_ANSI__
std::puts("strict ISO dialect");
#else
std::puts("GNU dialect");
#endif
std::printf("__cplusplus = %ld\n", static_cast<long>(__cplusplus));
}go deeper
Know that -std picks the C++ standard and that GCC 16 uses gnu++20 when you pass nothing.
Explain what the gnu dialects add, which macro marks strict mode, and why strict mode still needs -Wpedantic.
Anticipate the breakage a default-dialect change brings on a compiler upgrade, and use __cplusplus to see what a build actually selected.
Decide when the codebase moves to a new standard and enforce the choice explicitly in every build, rather than inheriting the compiler's default.
## The default, and why it moved GCC picks a C++ dialect even when you do not. In GCC 16 the manual says the default, if no C++ dialect option is given, is **`-std=gnu++20`**. libstdc++'s manual records the history: | GCC releases | Default C++ dialect | |---|---| | 6 to 10 | `-std=gnu++14` | | 11 to 15 | `-std=gnu++17` | | 16 | `-std=gnu++20` | The practical effect is that an unpinned build **changes language** on a compiler upgrade. Pinning `-std` in the build turns that into a deliberate decision. ## What moves when the default becomes C++20 The standard's own compatibility annex lists what C++20 changed against C++17. The items that most often surface when GCC 16's new default meets old code: - **New keywords**: `char8_t`, `concept`, `requires`, `consteval`, `constinit`, `co_await`, `co_yield` and `co_return`. A variable or function with one of those names stops compiling. - **`u8` literals**: `u8"text"` is now an array of `const char8_t`, so `const char* p = u8"text";` no longer compiles and `auto` deduces `const char8_t*`. - **Aggregates**: a class with any user-declared constructor is never an aggregate, so brace-initializing such a class member by member stops working. - **Comparisons**: `operator==` is also tried with its arguments reversed, which can pick a different overload or make a call ambiguous. None of these is a GCC quirk; they are C++20 rules, which the material on standard versions covers. GCC 16 simply applies them by default. ## What the gnu dialects add Each standard has two spellings: - `-std=c++17`, `-std=c++20`, `-std=c++23`: **ISO C++** plus amendments; - `-std=gnu++17`, `-std=gnu++20`, `-std=gnu++23`: the same standard **with GNU extensions**. The manual's description is careful: by default GCC provides extensions that on rare occasions conflict with the standard, and the strict `-std` options disable the ones that conflict. The visible differences are: 1. **`__STRICT_ANSI__`** is defined to `1` under a strict `-std`, and only then. Library headers use it to hide non-standard declarations. 2. **`typeof`** stops being a keyword, because strict dialects imply `-fno-gnu-keywords`; the reserved spelling `__typeof__` still works. 3. **System-specific predefined macros** outside the reserved namespace, such as `unix` or `linux`, are suppressed, while `__unix__` and `__linux__` remain. Code testing `#ifdef linux` silently takes the other branch. Everything else in the language is identical between the two spellings of one standard. ## Strict is not pedantic `-std=c++20` does **not** reject every extension. A variable-length array such as `int buf[n];` with a runtime `n` still compiles without a word. To be told about such extensions you need the warning options covered with diagnostics: - `-Wpedantic` issues all warnings demanded by strict ISO C++ for the selected `-std`; - `-pedantic-errors` makes them errors. So "portable ISO C++" usually means `-std=c++20 -Wpedantic` together, not either alone. ## Choosing and checking the standard - `__cplusplus` reports the selected standard: `201703L` for C++17, `202002L` for C++20, `202302L` for C++23. - Library features follow the standard too. In libstdc++, `std::print` is only declared from C++23, so `#include <print>` under `-std=c++20` leaves `std::print` undeclared. - GCC 16's manual still calls `c++23` support experimental and `c++26` highly experimental, and C++20 modules need `-fmodules`. - Build systems choose a spelling for you: CMake's `CXX_EXTENSIONS` property is `ON` by default, which yields the `gnu++` form. - `g++ -std=c++20 -dM -E -x c++ /dev/null | grep -E '__cplusplus|__STRICT_ANSI__'` shows what a given command line selects, without compiling anything. "Experimental" has a concrete meaning here. For `c++23` and `gnu++23`, the GCC 16 manual warns that support could change **in incompatible ways in future releases**. Code that compiles today may need changes after the next upgrade, which is why teams that adopt such a mode early tend to build everything with one compiler release instead of mixing in libraries prebuilt by another. The default `gnu++20` carries no such warning, apart from C++20 modules, which stay experimental behind `-fmodules`. ## A sensible policy 1. Always pass `-std` explicitly, in the build files rather than in someone's environment. 2. Use the strict spelling for library code that must also build with other compilers, and add `-Wpedantic` in CI. 3. Use a `gnu++` dialect only when you rely on a GNU extension, and say which one. 4. Upgrade the standard as a planned change, with `__cplusplus` checks where behaviour depends on it.
- Why can a GCC upgrade from 15 to 16 break a build that never passes -std?The implicit dialect moves from `gnu++17` to `gnu++20`. New keywords such as `concept` and `requires`, `char8_t` for `u8` literals and the rewritten comparison rules can turn valid C++17 into errors or change which overload is chosen. Pinning `-std` makes the switch deliberate.
- Does -std=c++20 alone guarantee code free of GNU extensions?No. It disables only extensions that conflict with the standard. Others, such as variable-length arrays, still compile silently; `-Wpedantic` or `-pedantic-errors` is what diagnoses them.
- GCC compiles C++98 unless you pass -std.
- -std=c++20 rejects every GNU extension, so -Wpedantic is redundant.
- gnu++20 is a different language revision from C++20 rather than C++20 plus extensions.
- The default dialect never changes between GCC releases.
- __STRICT_ANSI__ is defined in every GCC compilation.
Assumes GCC 16, whose default C++ dialect is gnu++20; GCC 11 to 15 defaulted to gnu++17.