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

Manuel picture Manuel · Jun 24, 2017 · Viewed 35.2k times · Source

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?

Answer

Michael Burr picture Michael Burr · Jun 24, 2017

If your version of g++ is later than 4.7 I think you can find the default version of C++ standard supported like so:

g++ -dM -E -x c++  /dev/null | grep -F __cplusplus

An example from my machine:

mburr@mint17 ~ $ g++ --version | head -1
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
mburr@mint17 ~ $ g++ -dM -E -x c++  /dev/null | grep -F __cplusplus
#define __cplusplus 199711L

Some references: