简体   繁体   中英

Why does g++ (4.8.2) use c++0x as default?

I have a code which uses std::unique_ptr supported since c++11.

#include <memory>

int main() {
        std::unique_ptr<int> p_int(new int(3));
        return 0;
}

I could build this code, but I'm still confused. Becuase my g++'s version is 4.8.2 which supports c++11. What's the reason that g++ uses c++0x as default? And how should I set c++11 as default? Now I'm using cmake, so maybe I should set c++11 in CMakeLists.txt..

$ which g++   
/usr/bin/g++
$ g++ --version
g++ (20140812 (SCEL u2.0.0.0)) 4.8.2
Copyright (C) 2013 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++ main.cc  
main.cc: In function ‘int main()’:
main.cc:4:2: error: ‘unique_ptr’ is not a member of ‘std’
  std::unique_ptr<int> p_int(new int(3));
  ^
main.cc:4:18: error: expected primary-expression before ‘int’
  std::unique_ptr<int> p_int(new int(3));
                  ^
main.cc:4:18: error: expected ‘;’ before ‘int’
$ g++ main.cc -std=c++11     // this is ok   

Althoguh my question is not so critical for its building, I want to clear my thought! Any helps will be appreciated. Thank you!

" What's the reason that g++ uses c++0x as default ": It doesn't . c++0x was the name for the working draft of c++11

g++ uses -std=gnu++98 by default. This is c++98 with GNU extensions added on top of it. Since this has been the default for a long time, it will likely remain the default for the foreseeable future to avoid breaking code that builds with it.

If you want to use c++11 as the default you can create an alias, in bash for example:

alias g++="g++ -std=c++11"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM