[Boost-bugs] [Boost C++ Libraries] #11739: operator / for point2 is hard coded to operate on type double

Subject: [Boost-bugs] [Boost C++ Libraries] #11739: operator / for point2 is hard coded to operate on type double
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2015-10-18 20:12:57


#11739: operator / for point2 is hard coded to operate on type double
------------------------------+---------------------
 Reporter: c.d.glover@… | Owner: hljin
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: GIL
  Version: Boost 1.59.0 | Severity: Problem
 Keywords: |
------------------------------+---------------------
 When using boost::gil::point2<float>, applying a division operation
 results in a point2<double>. This is undesirable.

 The fix appears to be easy, change this code

 template <typename T> BOOST_FORCEINLINE
 point2<double> operator/(const point2<T>& p, double t) { return t==0
 ? point2<double>(0,0):point2<double>(p.x/t,p.y/t); }

 to operate on T instead of double. But, that seems overly simple so I am
 trying to figure out why this code was written this way in the first
 place.

 A possibly safer alternative is to cast the result back to point2<T>, ie;

 template <typename T> BOOST_FORCEINLINE
 point2<double> operator/(const point2<T>& p, double t) { return t==0
 ? point2<T>(0,0):point2<T>(T(p.x/t),T(p.y/t)); }

 This change makes the / operator act the same as the /= operator, which is
 also hard coded to double, but because it's a member, doesn't yield a new
 type, so it's probably fine (other than the fact that I asked for floats
 and am getting double operations).

 While we're at it, I also find it weird that this code is protecting
 against division by zero, but that's a different problem.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/11739>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:19 UTC