Note that on MacOS with the Xcode developer tools, the g++ and clang compiler toolsets are just aliases to Apple Clang, which is Apple's version of Clang.
If you want to find out experimentally, you can get the C++ language standard version being used at compile-time using the standard __cplusplus
macro. Just compile a file that does a message pragma printing the value of __cplusplus
without specifying a c++ language version flag in the compile command.
But you may as well read the docs.
Defaults for GCC
See its documentation, which at the time of this writing states:
C++17 features are available since GCC 5. This mode is the default in GCC 11; it can be explicitly selected with the -std=c++17 command-line flag, or -std=gnu++17 to enable GNU extensions as well.
GCC has full support for the of the 2014 C++ standard. This mode is the default in GCC 6.1 up until GCC 10 (including); it can be explicitly selected with the -std=c++14 command-line flag, or -std=gnu++14 to enable GNU extensions as well.
GCC has full support for the 1998 C++ standard as modified by the 2003 technical corrigendum and some later defect reports, excluding the export feature which was later removed from the language. This mode is the default in GCC versions prior to 6.1; it can be explicitly selected with the -std=c++98 command-line flag, or -std=gnu++98 to enable GNU extensions as well.
Also related: Which C++ standard is the default when compiling with g++?
Defaults for Clang
See its documentation, which at the time of this writing states:
By default, Clang builds C++ code according to the C++14 standard. You can use Clang in C++14 mode with the -std=c++14 option (use -std=c++1y in Clang 3.4 and earlier).
The default C++ language version sometimes changes with newer releases of Clang, such as it did in Clang 6.0 from gnu++98 to gnu++14
Defaults for MSVC
See its documentation for the /std
flag, which at the time of this writing states:
/std:c++14
The /std:c++14
option enables C++14 standard-specific features implemented by the MSVC compiler. This option is the default for code compiled as C++. It's available starting in Visual Studio 2015 Update 3.
Changing the Defaults
The way to change the default depends on what other build tools you are using.
If you are using a Makefiles buildsystem, then use the CXXFLAGS
environment variable. See also the environment variable docs. Related: CFLAGS, CCFLAGS, CXXFLAGS - what exactly do these variables control?.
If you are using CMake, use the CXXFLAGS
environment variable. If you are the maintainer of a project and you know a specific target requires a specific language version, use target_compile_features
with a visibility modifier based on whether the requirement is for building the target, or using its headers, or both.
If you're not using a buildsystem but are invoking the compiler programs directly from a shell like Bash, you can create an alias like alias g++='g++ -std=c++20'
.
If you are using an IDE, check your IDE for settings related to setting the default language version for C++ projects. For example,
I'm not aware of a way to configure the compiler itself apart from doing something for/with the buildsystem or shell that you run the compiler with. You might need to actually build a modified version of the compiler if you actually want to go that deep.
Related question: Set as default C++11 in Clang.
That being said, for any code large enough to warrant using a buildsystem, since not all projects will have the same minimum language standard requirements, it's better to specify compiler-language-mode requirements for a project explicitly in the buildsystem's configuration files (for example, using target_compile_features
in CMake) so that users invoking the buildsystem for the project won't have to remember to manually set the right language-standard flags and can just let the buildsystem do what's needed.
Remember that for some projects (especially widely used-libraries), having lower requirements for language standard to build them or use their headers is a feature, because it allows people who (for their own reasons) cannot or choose not to use newer compilers (and instead use older ones that don't support or don't fully support newer language features) to build them or include headers from them.