How to use C++ 11 features in the latest version of g++

28,097

You don't need to upgrade. Specify the standards version to g++. For example, to compile an example program from cppreference.com:

$ g++ --version
g++ (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ --std=c++11 -o test test.cpp
$ ./test
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337
Share:
28,097

Related videos on Youtube

Pigna
Author by

Pigna

Updated on September 18, 2022

Comments

  • Pigna
    Pigna over 1 year

    Newbie here. I just got an error running from the terminal a C++ program I wrote: error: ‘stoi’ is not a member of ‘std’. They told me the compiler is too old.

    I'm using Ubuntu 14.04.

    My g++ version is 4.8.4.

    How do I upgrade?

  • Pigna
    Pigna over 8 years
    so every time I use g++ I need to add --std=c++11? Can't I make C++11 the default standard?
  • muru
    muru over 8 years
    @Pigna If you run g++ that often, you should be writing Makefiles.
  • charlestoncrabb
    charlestoncrabb almost 7 years
    @muru: If your machine pre-dates the standard then an update is required. Example: c++17/c++1z just released a few months ago, and my machine does not support the standard because its a few years old. Your answer is a solution to the OP's particular use case, not the posed question
  • muru
    muru almost 7 years
    @charlestoncrabb then you'd be looking for askubuntu.com/questions/466651/…. I don't really care about the posed question, because it's often the case that people think they want X when they want to solve Y.
  • mjwrazor
    mjwrazor almost 6 years
    @muru What if you are compiling someone else's code and can't edit the MakeFiles.
  • muru
    muru almost 6 years
    That's why well-written Makefiles support CC, CXX, CXXFLAGS and the like.