I have a piece of code that looks like the following. Let\'s say it\'s in a file named example.cpp
#include
#include
The default language standards for both C and C++ are specified in the GCC Manuals. You can find these as follows:
Browse to https://gcc.gnu.org/onlinedocs/
Select the GCC #.## Manual link for the version of GCC you are interested in, e.g. for GCC 7.5.0:
https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/
Click the topic link Language Standards Supported by GCC, followed by the topic C++ Language (or C language). Either of these topics will have a sentence such as:
The default, if no C++ language dialect options are given, is -std=gnu++14.
The default, if no C language dialect options are given, is -std=gnu11.
The above two examples are for GCC 7.5.0.
Typing g++ --version
in your command shell will reveal the version of the compiler, and from that you can infer the default standard. So you can't tell directly but you can infer it, with some effort.
Compilers are supposed to #define
__cplusplus
which can be used to extract the standard that they purport to implement at compile time; but many don't do this yet.
(And don't forget to include all the C++ standard library headers you need: where is the one for std::string
for example? Don't rely on your C++ standard library implementation including other headers automatically - in doing that you are not writing portable C++.)
man g++ | grep "This is the default for C++ code"
Your question is specific to gnu compilers, so probably better to tag it appropriately, rather than just C++ and C++11.
Your code will compile with any compilers (and associated libraries) compliant with C++11 and later.
The reason is that C++11 introduced a std::ifstream
constructor that accepts a const std::string &
. Before C++11, a std::string
could not be passed, and it would be necessary in your code to pass filename.c_str()
rather than filename
.
According to information from gnu, https://gcc.gnu.org/projects/cxx-status.html#cxx11, gcc.4.8.1 was the first version to fully support C++11. At the command line g++ -v
will prod g++
to telling you its version number.
If you dig into documentation, you might be able to find the version/subversion that first supported enough features so your code - as given - would compile. But such a version would support some C++11 features and not others.
Since windows isn't distributed with g++, you will have whatever version someone (you?) has chosen to install. There will be no default version of g++ associated with your version of windows.
I believe that it is possible to tell by looking at the man page (at least for g++):
Under the description of -std
, the man page lists all C++ standards, including the GNU dialects. Under one specific standard, it is rather inconspicuously stated, This is the default for C++ code.
(there is an analogous statement for C standards: This is the default for C code.
).
For instance, for g++/gcc version 5.4.0
, this is listed under gnu++98/gnu++03
, whereas for g++/gcc version 6.4.0
, this is listed under gnu++14
.
You can also check with gdb
$ g++ example.cpp -g
Compile program with -g flag to generate debug info$ gdb a.out
Debug program with gdb(gdb) b main
Put a breakpoint at main(gdb) run
Run program (will pause at breakpoint)(gdb) info source
Prints out something like:
Current source file is example.cpp
Compilation directory is /home/xxx/cpp
Located in /home/xxx/cpp/example.cpp
Contains 7 lines.
Source language is c++.
Producer is GNU C++14 6.3.0 20170516 -mtune=generic -march=x86-64 -g.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.
There is the standard used by compiler: Producer is GNU C++14
If you recompile your program using -std=c++11
(for example), gdb detects it:
Producer is GNU C++11