如果我包含"-ansi"编译器选项,为什么我的C++ 0x代码无法编译?

ano*_*non 11 c++ g++ ansi c++11

我遇到了一个非常奇怪的错误,只有在我使用ansi旗帜时才弹出.

#include <memory>

class Test
{
  public:
    explicit Test(std::shared_ptr<double> ptr) {}
};
Run Code Online (Sandbox Code Playgroud)

这是使用gcc 4.5.2和4.6.0(20101127)测试的编译:

g++ -std=c++0x -Wall -pedantic -ansi test.cpp
test.cpp:6:34: error: expected ')' before '<' token
Run Code Online (Sandbox Code Playgroud)

但编译没有-ansi工作.为什么?

zwo*_*wol 10

对于GNU C++编译器,-ansi是另一个名称-std=c++98,它覆盖了-std=c++0x之前在命令行上的名称.你可能只想要

$ g++ -std=c++0x -Wall minimal.cpp
Run Code Online (Sandbox Code Playgroud)

(-pedantic默认情况下,对于C++是打开的,所以不必再说一遍.如果你想要选择警告,请尝试添加-Wextra.)