http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49793

           Summary: Narrowing conversion from int to double with
                    -std=c++0x
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: bugzilla.gcc.gnu....@sklep.czarko.net


This code compiles fine with -ansi but fails with -std=c++0x:

struct Size
{
    double w;
    double h;
};

int foo( const int a )
{
    return a;
}

int main()
{
    const int    left    = foo( 12 );
    const int    right   = foo( 33 );
    const int    bottom  = foo( 11 );
    const int    top     = foo( 99 );
    const int    width   = right - left;
    const int    height  = top - bottom;
    const int    l       = 12;
    const int    r       = 33;
    const int    b       = 11;
    const int    t       = 99;
    const int    w       = r - l;
    const int    h       = t - b;
    const double wd      = right - left;
    const double ht      = top - bottom;

    Size size1;
    size1.w = right - left;
    size1.h = top - bottom;

    const Size size2 = { r - l, t - b };

    const Size size3 = { w, h };

    const Size size4 = { wd, ht };

    const Size size5 = { width, height };

    const Size size6 = { right - left, top - bottom };

    return 0;
}

g++ -std=c++0x -o a a.cpp
a.cpp: In function ‘int main()’:
a.cpp:39:40: error: narrowing conversion of ‘width’ from ‘const int’ to
‘double’ inside { } [-fpermissive]
a.cpp:39:40: error: narrowing conversion of ‘height’ from ‘const int’ to
‘double’ inside { } [-fpermissive]
a.cpp:41:53: error: narrowing conversion of ‘(((int)right) - ((int)left))’ from
‘int’ to ‘double’ inside { } [-fpermissive]
a.cpp:41:53: error: narrowing conversion of ‘(((int)top) - ((int)bottom))’ from
‘int’ to ‘double’ inside { } [-fpermissive]


It looks like compiler accepts some conversions from int to double as valid but
it complains about others at the same time.

If const is removed from the code then additional two errors are reported:

a.cpp:33:39: error: narrowing conversion of ‘(r - l)’ from ‘int’ to ‘double’
inside { } [-fpermissive]
a.cpp:33:39: error: narrowing conversion of ‘(t - b)’ from ‘int’ to ‘double’
inside { } [-fpermissive]
a.cpp:35:31: error: narrowing conversion of ‘w’ from ‘int’ to ‘double’ inside {
} [-fpermissive]
a.cpp:35:31: error: narrowing conversion of ‘h’ from ‘int’ to ‘double’ inside {
} [-fpermissive]


If I replace int with short then the errors which are reported are:

a.cpp:39:40: error: narrowing conversion of ‘width’ from ‘const short int’ to
‘double’ inside { } [-fpermissive]
a.cpp:39:40: error: narrowing conversion of ‘height’ from ‘const short int’ to
‘double’ inside { } [-fpermissive]
a.cpp:41:53: error: narrowing conversion of ‘(((int)right) - ((int)left))’ from
‘int’ to ‘double’ inside { } [-fpermissive]
a.cpp:41:53: error: narrowing conversion of ‘(((int)top) - ((int)bottom))’ from
‘int’ to ‘double’ inside { } [-fpermissive]

and with char instead of int:

a.cpp:39:40: error: narrowing conversion of ‘width’ from ‘const char’ to
‘double’ inside { } [-fpermissive]
a.cpp:39:40: error: narrowing conversion of ‘height’ from ‘const char’ to
‘double’ inside { } [-fpermissive]
a.cpp:41:53: error: narrowing conversion of ‘(((int)right) - ((int)left))’ from
‘int’ to ‘double’ inside { } [-fpermissive]
a.cpp:41:53: error: narrowing conversion of ‘(((int)top) - ((int)bottom))’ from
‘int’ to ‘double’ inside { } [-fpermissive]

Reply via email to