What does the GCC or Clang flag -std=c++17 select, and how does it differ from -std=gnu++17 and from passing no flag?
answer
- one language mode per translation unit
- ISO mode versus GNU dialect
- __STRICT_ANSI__ and typeof
- defaults differ by compiler and release
basics
~20 s-std=c++17 compiles the translation unit as ISO C++17, setting __cplusplus to 201703L and disabling GNU extensions that conflict with it. -std=gnu++17 is C++17 plus all GNU extensions. With no flag, each compiler uses its own default mode.
solid answer
~40 s`-std=` picks the **language mode** for a translation unit: which grammar and library declarations are visible and what `__cplusplus` reports (`201703L` for C++17). `-std=c++17` is the **ISO mode**: GCC's manual says it accepts C++17 programs plus GNU extensions that do not contradict the standard, so a keyword such as `typeof` disappears and `__STRICT_ANSI__` is defined. `-std=gnu++17` is the **GNU dialect** of the same revision, with every extension on. With no flag you get the compiler's default, and defaults differ: GCC 16 documents `gnu++20`, upstream Clang documents `gnu++17`, and Apple clang 21 builds as C++14 with GNU extensions. The flag never adds features a compiler has not implemented. MSVC spells the choice `/std:c++17`, `/std:c++20` or `/std:c++latest`.
code
cpp · 13 lines#include <cstdio>
int main() {
std::printf("__cplusplus = %ld\n", static_cast<long>(__cplusplus));
#ifdef __STRICT_ANSI__
std::puts("ISO mode: __STRICT_ANSI__ is defined");
#else
std::puts("GNU mode: __STRICT_ANSI__ is not defined");
#endif
#if __cplusplus >= 201703L
std::puts("language mode is C++17 or later");
#endif
}go deeper
Know that -std=c++17 selects C++17 for the file being compiled and sets __cplusplus to 201703L, and that MSVC spells the same choice /std:c++17.
Explain ISO mode versus GNU dialect with an example such as typeof, and why defaults differ across GCC, upstream Clang and Apple clang and move between releases.
Show you have debugged a build where one target compiled in a different mode than intended, and how you found it from the real compiler command line.
Decide the house rule: an explicit ISO mode on every target, set once in the build system, with GNU extensions allowed only where a reviewed exception exists.
## What the flag selects A C++ compiler can compile the same source under several **language modes**, one per revision of the standard. The `-std=` flag on GCC and Clang chooses that mode for each translation unit it compiles. The mode decides: - which **grammar** the parser accepts — `concept` is a keyword in C++20 mode and an ordinary identifier in C++17 mode; - which **library declarations** the standard headers expose — `<optional>` declares `std::optional` only from C++17 on, and libc++ gives `std::format` only from C++20; - the value of **`__cplusplus`** and of the language and library **feature-test macros**; - a few **rule changes** that alter the meaning of code that compiles either way, such as C++17's guaranteed copy elision. The flag does *not* make a compiler implement anything it does not already implement. Asking an old compiler for `-std=c++20` either fails with an unknown-value error or gives you its partial, often experimental, idea of C++20. ## ISO modes and GNU dialects GCC and Clang offer each revision twice: | Flag | Meaning | `__STRICT_ANSI__` | |---|---|---| | `-std=c++17` | ISO C++17; GNU extensions that conflict with the standard are off | defined | | `-std=gnu++17` | C++17 with every GNU extension enabled | not defined | GCC's manual states the rule: with a base standard, the compiler accepts all programs following that standard plus those using GNU extensions that do not contradict it; with a GNU dialect, all supported features are enabled even when they change the meaning of the base standard. A concrete case: `typeof(1) x = 2;` compiles under `-std=gnu++17` and fails under `-std=c++17` with `use of undeclared identifier 'typeof'`. Extensions that do not conflict, such as `__attribute__`, keep working in both. `-Wpedantic` uses the selected standard to decide what counts as an extension, so portable code builds with the ISO spelling and `-Wpedantic`. ## Passing no flag Without `-std=`, the compiler uses its **default mode**, and that default is a property of the compiler *and* of its release: - GCC 16's manual documents `gnu++20` as the default for C++ code. - Upstream Clang's command guide documents `gnu++17`. - Apple clang 21, the compiler used to check this answer, reports `__cplusplus` as `201402L` with no flag: C++14 with GNU extensions. - MSVC has its own switch, `/std:`, with values such as `c++14`, `c++17`, `c++20` and `c++latest`. So a project that relies on the default builds as three different languages on three toolchains, and silently changes language when a toolchain is upgraded. Setting the mode explicitly — on the command line or through the build system — is the fix. ## Provisional and future spellings Each revision also has a pre-publication name: `c++1z` for C++17, `c++2a` for C++20, `c++2b` for C++23 and `c++2c` for C++26. GCC 16 documents the old names as deprecated and describes its `-std=c++23` support as experimental and `-std=c++2c` as highly experimental. A provisional mode is a moving target: Apple clang 21 in `-std=c++2c` mode reports `__cplusplus` as `202400L`, not the `202603L` of the C++26 final draft. ## Where the flag really comes from In most projects nobody types `-std=` by hand: the build system adds it. CMake, Meson, Bazel and IDE project files each translate a "C++ standard" setting into `-std=c++17` for GCC and Clang or `/std:c++17` for MSVC, and many of them also choose between the ISO and GNU spellings — CMake, for instance, picks the GNU dialect unless extensions are switched off. When a build behaves as if it used the wrong standard, read the **actual compiler command line** from the build's verbose output or its compilation database: a global flag appended after a target's own setting, or a dependency that brings its own flags, is a common culprit. Because GCC and Clang take the last `-std=` they see, the order of flags matters as much as their presence. ## Practical rules 1. Set the standard **explicitly** for every target; never rely on the compiler default. 2. Prefer the **ISO spelling** (`c++17`, not `gnu++17`) unless you knowingly use a GNU extension. 3. Use the **same mode for every translation unit** linked into one program, including prebuilt dependencies. 4. Remember the flag selects a mode; whether a given feature works in that mode is a separate question, answered by the compiler's status pages and the feature-test macros.
- If both -std=c++17 and -std=c++20 appear on one compiler command line, which mode wins?With GCC and Clang the last `-std=` on the command line wins, so the order in which a build system appends flags decides the mode. That is a common source of a target silently compiling as an older standard: a global flag added after the target's own. Check the real command line (for example from the build's verbose output or `compile_commands.json`) rather than the build script.
- Is it safe to build one library with -std=gnu++17 and the program that links it with -std=c++17?Usually it works, because both flags select the same revision. The two modes are close but not byte-for-byte identical: libstdc++ tests `__STRICT_ANSI__` in a number of headers, mostly around extension types. The real risk is mixing *revisions* — C++17 and C++20 — which can change library declarations and break linking. One consistent mode for every target removes the question.
- Without a -std flag every compiler builds the newest standard it supports.
- -std=c++20 makes an old compiler implement the C++20 features it lacks.
- -std=gnu++17 is a different language revision from C++17.
- Compiler defaults never change between releases, so relying on them is safe.
- -std=c++17 turns off every GNU extension, including attributes.