简体   繁体   English

尽管有更新,clang会对c ++ 11发出警告

[英]clang produces warning regarding c++11 despite update

updated clang recently (as well as xcode and developer tools) and ran a simple program to see if it was supporting c++11. 最近更新了clang(以及xcode和开发人员工具)并运行了一个简单的程序来查看它是否支持c ++ 11。 Look like this: 看起来像这样:

#include <iostream>

using namespace std;

    int main()
    {
        string my_array[5] = {"one", "two", "three"};

        for (string &x : my_array)
            cout << x << endl;
    }

compile in terminal like this: 像这样在终端编译:

clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp

and get this warning: 并得到这个警告:

main.cpp:17:20: warning: range-based for loop is incompatible with C++98
      [-Wc++98-compat]
    for (string &x : my_array)

but it still produces an executable and runs as expected. 但它仍然生成一个可执行文件并按预期运行。 Why is this error being produced? 为什么会产生这个错误?

This is a warning rather than an error. 这是警告而不是错误。 The warning message also indicates the warning flag that enables it: -Wc++98-compat. 警告消息还指示启用它的警告标志:-Wc ++ 98-compat。 This flag is enabled because you've enabled -Weverything (a good idea, IMO). 此标志已启用,因为您已启用-Weverything(一个好主意,IMO)。 To disable a specific warning you pass a warning flag with 'no-' prefixed to the warning name you want to disable: 要禁用特定警告,请传递警告标志,并在要禁用的警告名称前加上“no-”前缀:

-Wno-c++98-compat

The purpose of this warning is to allow building code as C++11 and to gain some C++11 benefits, such as performance improvements from move semantics, while still producing code compatible with older compilers. 此警告的目的是允许构建代码为C ++ 11并获得一些C ++ 11的好处,例如移动语义的性能改进,同时仍然生成与旧编译器兼容的代码。 That is, this warning doesn't indicate any kind of problem with the program and the program will work just as the C++11 spec indicates (other than clang bugs, of course), but the warning is to inform you that if you were to compile as C++98 then it would not work. 也就是说,这个警告并不表示程序有任何问题,程序就像C ++ 11规范所指出的那样(当然除了铿锵声),但警告是告诉你,如果你要编译为C ++ 98然后它将无法正常工作。

If you don't plan to build the code as C++98 then this warning doesn't have any value to you and you should simply disable it. 如果您不打算将代码构建为C ++ 98,那么此警告对您没有任何价值,您应该只是禁用它。

I believe there's also a -Wc++11-compat flag in the latest versions now that clang supports (what will probably be called) C++14. 我相信在最新版本中还有一个-Wc ++ 11-compat标志,现在clang支持(可能会被称为)C ++ 14。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM