为什么g ++(4.8.2)使用c ++ 0x作为默认值?

jef*_*jef 0 c++ g++ smart-pointers c++11

我有一个代码,它使用自c ++ 11以来支持的std :: unique_ptr.

#include <memory>

int main() {
        std::unique_ptr<int> p_int(new int(3));
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

我可以构建这个代码,但我仍然感到困惑.因为我的g ++版本是4.8.2,它支持c ++ 11.g ++使用c ++ 0x作为默认值的原因是什么?我应该如何将c ++ 11设置为默认值?现在我正在使用cmake,所以也许我应该在CMakeLists.txt中设置c ++ 11 ..

$ 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   
Run Code Online (Sandbox Code Playgroud)

Althoguh我的问题对它的建筑并不那么重要,我想清除我的想法!任何帮助将不胜感激.谢谢!

Rya*_*ing 5

" g ++使用c ++ 0x作为默认值的原因是什么 ":它没有.c++0x是工作草案的名称c++11

g ++ -std=gnu++98默认使用.这是c ++ 98,其上添加了GNU扩展.由于这是很长时间的默认设置,因此在可预见的未来可能仍然是默认设置,以避免破坏使用它构建的代码.

如果你想c++11用作默认值,你可以在bash中创建一个别名,例如:

alias g++="g++ -std=c++11"
Run Code Online (Sandbox Code Playgroud)