G++ narrowing conversions with C++98

I have a C++98 project on linux with g++ 4.7.3 and I'm getting lots of warnings about "narrowing conversion is ill-formed in C++11"

How come? I'm not even using C++11

Answers


Those warnings are "this is valid now, but won't be valid in the future" warnings. They're specifically designed for code that is valid C++98, that won't cause a compilation error in C++98 mode, but would cause problems if you intend to switch to C++11 in the future. They don't get enabled by default, but do by -Wall. If you're really sure that that isn't going to happen, you can change -Wall to -Wall -Wno-c++11-compat

Example code:

unsigned u[] = {-1};
$ g++ -c test.cc -ansi -pedantic
$ g++ -c test.cc -ansi -pedantic -Wall
test.cc:1:19: warning: narrowing conversion of ‘-1’ from ‘int’ to ‘unsigned int’ inside { } is ill-formed in C++11 [-Wnarrowing]
 unsigned u[] = {-1};
                   ^
$ g++ -c test.cc -ansi -pedantic -Wall -Wno-c++11-compat

Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.