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

Richard legalize at xmission.com
Mon May 11 10:03:50 PDT 2015


In article <55507506.9070603 at gmx.net>,
    Dennis Luehring <dl.soluz at gmx.net> writes:

> #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]
>    }

See section 2.14.2 Integer literals [lex.icon] in the standard.  The
literal 0xFFFA0001 is too large to fit in an int (assuming int is
32-bits since you got an error about narrowing) because it extends
into the sign bit for a 32-bit integer.  The largest 32-bit positive
integer is 0x7FFFFFFF.  Your literal was turned into a long int as per
the above section of the standard.
-- 
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
         The Terminals Wiki <http://terminals.classiccmp.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>



More information about the cfe-dev mailing list