如何使用-Wall -02和-std=c++0x进行编译

How to compile with -Wall -02 and -std=c++0x

本文关键字:c++0x 编译 -std 何使用 -Wall      更新时间:2023-10-16

我想用- Wall -O2-std=c++0x编译。当我编译没有-Wall -02我有:typeof was undeclared in this scopeit was undeclared in this scope。当我编译我的代码没有需要-std=c++0x的代码的一部分,一切都很好,但我想要这部分。怎么了?

需要- Wall -O2:

的代码
for(typeof(g[node].begin()) it = g[node].begin(); it != g[node].end(); ++it) 
需要-std=c++0x: 的代码

auto biggest = std::max_element(std::begin(koszty), std::end(koszty));

avg = accumulate(czasy.begin(), czasy.end(), 0) / czasy.size();

没有命名为typeof的操作符、关键字或标准函数。似乎你正在尝试使用c++ 11 decltype结构。

使用auto:

使用c++ 11的类型演绎可能更好。
for(auto it = g[node].begin(); it != g[node].end(); ++it) 

或者是基于范围的for循环:

for (auto& val : g[node]) { ... }

就像注释所说的那样,你想使用的选项不是不兼容的,如果需要或想要使用它们,请使用它们。

ISO c++中没有提到标识符typeof。它是C和c++的GNU扩展。它与-Wall(控制警告的选项)或-O2(控制优化的选项)无关。如果确实需要,您应该使用-std=gnu++0x而不是-std=c++0x来启用GNU方言。

然而,这似乎是不必要的。由于您使用的是c++ 0x(现在是c++ 11),关键字decltype是正确且符合typeof的替代品。但是为了清晰起见,我建议使用auto