This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/49793] New: Narrowing conversion from int to double with -std=c++0x


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: unassigned@gcc.gnu.org
        ReportedBy: bugzilla.gcc.gnu.com@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]


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]