【问题标题】:Why does my C++0x code fail to compile if I include the "-ansi" compiler option?如果我包含“-ansi”编译器选项,为什么我的 C++0x 代码无法编译?
【发布时间】:2011-05-02 19:45:30
【问题描述】:

我遇到了一个非常奇怪的错误,只有在我使用 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 的编译工作。为什么?

【问题讨论】:

  • @Rob 实际上,只有第一个 (minimal.hpp) 是必需的。

标签: c++ c++11 g++ ansi


【解决方案1】:

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

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

-pedantic 在 C++ 中默认开启,因此无需重复。如果您想要更挑剔的警告,请尝试添加 -Wextra。)

【讨论】:

  • 谢谢,我一直在使用相同的选项这么长时间,并没有想过 -ansi 到底做了什么,只是开始使用新标准的功能。
  • "-pedantic 在 C++ 中默认开启" 是吗?
  • @LightnessRacesinOrbit 是的,文档在这一点上有点不清楚,但只要我记得,它就是这样。更准确地说,-pedantic-errors 是 C++ 中的默认值,-fpermissive 模式与 C 中的 -pedantic 大致相同;我不确定是否可以在 C++ 中禁用这些警告。
  • gcc.gnu.org/onlinedocs/gcc/Standards.html “要获得标准要求的所有诊断,您还应该指定 -pedantic(如果您希望它们是错误而不是警告,则应指定 -pedantic-errors)。” 对我来说似乎很清楚。我的经验是添加-pedantic 会产生更多警告。所以我必须不同意你的观点。
  • @LightnessRacesinOrbit 如果是这样,我很确定这是无意的,您应该提交错误报告。
【解决方案2】:

std::shared_ptr 在 c++98 中不存在。尝试以下更改:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2014-10-03
      • 1970-01-01
      • 2021-06-11
      • 2013-06-10
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      相关资源
      最近更新 更多