我遇到了一个非常奇怪的错误,只有在我使用
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
的编译工作。为什么?
对于 GNU C++ 编译器,
-ansi
是 -std=c++98
的另一个名称,它会覆盖您之前在命令行中使用的 -std=c++0x
。你可能只想
$ g++ -std=c++0x -Wall minimal.cpp
(
-pedantic
C++ 默认开启,所以不用再说了。如果你想要更挑剔的警告,尝试添加-Wextra
。)
std::shared_ptr
在 c++98 中不存在。试试这些改变:
#include <tr1/memory>
...
explicit Test(std::tr1::shared_ptr<double> ptr) {}
嗯,因为还没有 C++0x 的 ANSI 标准? ANSI 标志检查是否符合现有标准,而不是未来的标准。