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

问题描述 投票:0回答:3

我遇到了一个非常奇怪的错误,只有在我使用

ansi
标志时才会弹出。

#include <memory>

class Test
{
  public:
    explicit Test(std::shared_ptr<double> ptr) {}
};

这是编译,使用 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

但是没有

-ansi
的编译工作。为什么?

c++ c++11 g++ ansi-c
3个回答
10
投票

对于 GNU C++ 编译器,

-ansi
-std=c++98
的另一个名称,它会覆盖您之前在命令行中使用的
-std=c++0x
。你可能只想

$ g++ -std=c++0x -Wall minimal.cpp

-pedantic
C++ 默认开启,所以不用再说了。如果你想要更挑剔的警告,尝试添加
-Wextra
。)


3
投票

std::shared_ptr
在 c++98 中不存在。试试这些改变:

#include <tr1/memory>
...
explicit Test(std::tr1::shared_ptr<double> ptr) {}   

0
投票

嗯,因为还没有 C++0x 的 ANSI 标准? ANSI 标志检查是否符合现有标准,而不是未来的标准。

© www.soinside.com 2019 - 2024. All rights reserved.