[cfe-dev] why do i get the "c++11-narrowing" warning only in switch using defines?

James Dennett james.dennett at gmail.com
Mon May 11 11:13:17 PDT 2015


On Mon, May 11, 2015 at 4:23 AM, Dennis Luehring <dl.soluz at gmx.net> wrote:

> #define TEST0 0xFFFA0000
> #define TEST1 0xFFFA0001
>
> const int test0 = 0xFFFA0000;
> const int test1 = 0xFFFA0001;
>
> int main(int argc, char** arcv)
> {
>   if(argc == TEST0) // OK
>   {
>     return 1;
>   }
>
>   if(argc == test0) // OK
>   {
>     return 1;
>   }
>
>   switch(argc)
>   {
>     case TEST1: return 2; // ERROR: case value evaluates to 4294574081,
> which cannot be narrowed to type 'int' [-Wc++11-narrowing]
>   }
>
>   switch(argc)
>   {
>     case test1: return 2; // OK
>   }
> }
>

This isn't really the right list for this, but...

In the `if` statements, integral promotions apply, and the resulting type
can be other than `int`.  In the switch, the case labels must be converted
to match the type of the initial `switch` expression (here, `int`) -- and
the narrowing conversion from a constant is not permitted there.

-- James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150511/a98f8e6c/attachment.html>


More information about the cfe-dev mailing list