the - std=c++17

Which C++ standard is the default when compiling with g++? (5)

I'm guessing a default version of the C++ compiler gets called, but I don't know which?

This is only guessable by reading the documentation of your particular compiler version.

If using a recent GCC, I recommend first to understand what version are you using by running

g++ -v


g++ --version

and then refer to the version of the particular release of GCC. For example for GCC 7, read GCC 7 changes etc

Alternatively, run

g++ -dumpspecs

and decipher the default so called spec file.

BTW, you could ensure (e.g. in some of your common header file) that C++ is at least C++17 by coding

 #if __cplusplus < 201412L
 #error expecting C++17 standard

and I actually recommend doing it that way.

PS. Actually, think of C++98 & C++17 being two different languages (e.g. like Ocaml4 and C++11 are). Require your user to have a compiler supporting some defined language standard (e.g. C++11), not some particular version of GCC. Read also about package managers.

I have a piece of code that looks like the following. Let's say it's in a file named example.cpp

#include <fstream>
#include <string> // line added after edit for clarity

int main() {
    std::string filename = "input.txt";
    std::ifstream in(filename);

    return 0;

On a windows, if I type in the cmd the command g++ example.cpp, it will fail. It's a long list of errors I think mostly due to the linker complaining about not being able to convert from string to const char*.

But if I run the compiler using an additional argument like so: g++ -std=c++17 example.cpp, it will compile and work fine with no problems.

What happens when I run the former command? I'm guessing a default version standard of the C++ compiler gets called, but I don't know which? And as a programmer/developer, should I always use the latter command with the extra argument?

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.

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++.)

You can also check with gdb

  1. $ g++ example.cpp -g Compile program with -g flag to generate debug info
  2. $ gdb a.out Debug program with gdb
  3. (gdb) b main Put a breakpoint at main
  4. (gdb) run Run program (will pause at breakpoint)
  5. (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

g++ man page actually tells what is the default standard for C++ code.

Use following script to show the relevant part:

man g++ | col -b | grep -B 1 -e '-std.* default'

For example, in RHEL 6 g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), the output:

           GNU dialect of -std=c++98.  This is the default for C++ code.

And in Fedora 28 g++ (GCC) 8.1.1 20180502 (Red Hat 8.1.1-1), the output:

           GNU dialect of -std=c++14.  This is the default for C++ code.  The name gnu++1y is deprecated.