Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 541
E.T. Avatar asked Dec 06 '22 03:12

E.T.


1 Answers

This is a warning rather than an error. The warning message also indicates the warning flag that enables it: -Wc++98-compat. This flag is enabled because you've enabled -Weverything (a good idea, IMO). To disable a specific warning you pass a warning flag with 'no-' prefixed to the warning name you want to disable:

-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. 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.

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.

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.

like image 50
bames53 Avatar answered Dec 08 '22 17:12

bames53