geometry icon indicating copy to clipboard operation
geometry copied to clipboard

MSVC Warning: C4244 'initializing': conversion from 'CoordinateType' to 'const PromotedType', possible loss of data

Open AlastairHolmes opened this issue 5 years ago • 9 comments

Minimal Code to produce related warning (boost 1.70.0 MSVC 2017 15.8.0 /std:c++latest /W4):

boost::geometry::model::polygon<
    boost::geometry::model::point<std::int64_t, 3, boost::geometry::cs::cartesian>
> test;
boost::geometry::remove_spikes(test);

If the std::int64_t in the above is changed to int there is no warning.

On line 245 (side_by_triangle.hpp) boost decides the type of PromotedType = select_most_precise<double,std::int64_t> => double. And then initializes PromotedType values with std::int64_t values.

This pattern of select_most_precise<double,...> is pretty common, and I have found the same warning being produced in other parts of boost that do the same thing when using with std::int64_t.

AlastairHolmes avatar Oct 24 '19 11:10 AlastairHolmes

This is still a problem in boost 1.72.0. \boost_1_72_0\boost\geometry\strategies\cartesian\side_by_triangle.hpp(123): error C4244: 'initializing': conversion from 'CoordinateType' to 'PromotedType', possible loss of data

vschoech avatar Mar 09 '20 10:03 vschoech

This is still a problem in boost 1.73.0. \boost_1_73_0\boost\geometry\strategies\cartesian\side_by_triangle.hpp(123): error C4244: 'initializing': conversion from 'CoordinateType' to 'PromotedType', possible loss of data

vschoech avatar Jun 17 '20 09:06 vschoech

I think it should be safe to ... = static_cast<PromotedType>(...) wherever the left-hand side is an explicitly promoted type.

@awulkiew What do you think?

mloskot avatar Jun 18 '20 15:06 mloskot

Hello, I have a similar problem on Windows using msvc14.1. On linux there is no warning by the way.

I created a minimal example:

#include <boost/geometry.hpp>

namespace bg = boost::geometry;
typedef bg::model::polygon<bg::model::d2::point_xy<double>> polygon_t;
typedef bg::model::multi_polygon<polygon_t> mpolygon_t;

int main() {
    polygon_t p0{ {{-5,-5},{-5,5},{5,5},{5,-5},{-5,-5}} };
    polygon_t p1{ {{-4,-4},{-4,4},{4,4},{4,-4},{-4,-4}} };
    mpolygon_t out;

    bg::difference(p0, p1, out);
}

I than tracked it back to point_is_spike_or_equal.hpp line 142-146 and added the following lines to get some debugging output:

static_assert(Point1::foo);
static_assert(robust_point_type::foo);

And got the following errors:

1>...\boost\geometry\algorithms\detail\point_is_spike_or_equal.hpp(148): error C2039: 'foo': is not a member of 'boost::geometry::model::d2::point_xy<double,boost::geometry::cs::cartesian>'
1>...\consoleapplication1.cpp(9): note: see declaration of 'boost::geometry::model::d2::point_xy<double,boost::geometry::cs::cartesian>'
1>...\boost\geometry\algorithms\detail\point_is_spike_or_equal.hpp(148): error C2065: 'foo': undeclared identifier
1>...\boost\geometry\algorithms\detail\point_is_spike_or_equal.hpp(149): error C2039: 'foo': is not a member of 'boost::geometry::model::point<__int64,2,boost::geometry::cs::cartesian>'
1>...\boost\geometry\policies\robustness\get_rescale_policy.hpp(188): note: see declaration of 'boost::geometry::model::point<__int64,2,boost::geometry::cs::cartesian>'
1>...\boost\geometry\algorithms\detail\point_is_spike_or_equal.hpp(149): error C2065: 'foo': undeclared identifier

Where the 4th line is the interesting one. Why is the coordinate_type of boost::geometry::model::point an __int64. robust_point_type::type should have the same type as Point1 in the 2nd line.

Ne94fets avatar Jun 21 '20 14:06 Ne94fets

This is still a problem in 1.75.0. \boost_1_75_0\boost\geometry\strategies\cartesian\side_by_triangle.hpp(123,37): error C4244: 'initializing': conversion from 'CoordinateType' to 'PromotedType', possible loss of data

vschoech avatar Mar 26 '21 14:03 vschoech

This is still a problem in 1.79.0. \boost_1_79_0\boost\geometry\strategy\cartesian\side_by_triangle.hpp(102,37): warning C4244: 'initializing': conversion from 'CoordinateType' to 'PromotedType', possible loss of data

wanghan02 avatar Jul 06 '22 09:07 wanghan02

Seems unmaintained.

yoavmil avatar Jul 06 '22 12:07 yoavmil

This is still a problem in 1.83.0. boost_1_83_0\boost\geometry\strategy\cartesian\side_by_triangle.hpp(103,37): error C4244: 'initializing': conversion from 'CoordinateType' to 'PromotedType', possible loss of data

wanghan02 avatar Aug 28 '23 12:08 wanghan02

still problem in 1.84.0

boost_1_84_0\boost\range\irange.hpp(168,32): warning C4244: 'return': conversion from '__int64' to 'int', possible loss of data

leewi9 avatar Mar 05 '24 15:03 leewi9