Wednesday, 15 April 2015

c++ - Overloading comparison for specific integral constants -



c++ - Overloading comparison for specific integral constants -

i in process of converting big set of geospatial code 1 projection another. enforce proper units during conversion, have introduced distance, point, rectangle, , polygon templates take tag indicating coordinate scheme used. working out well, there lot of places checks performed non-zero (!= 0) or positive values (> 0). able overload these operators allow comparing against 0 without comparing against other number. possible accomplish this?

as additional restriction, cannot utilize constexpr because must back upwards vs 2013, i'd still interested hear if there way constexpr

just reference, working this:

template<typename tag> struct distance { int value; }; template<typename tag> struct point { distance<tag> x; distance<tag> y; }; // works fine comparing 2 distances template<typename tag> bool operator>(const distance<tag>& a, const distance<tag>& b) {return a.value > b.value;} // don't want allow > 14, > 0 template<typename tag> bool operator>(const distance<tag>& a, int b) {return a.value > b;} struct mercator; typedef point<mercator> mercpoint; struct guiscale; typedef point<guiscale> guipoint; // etc.

you may utilize nullptr_t hack (as literal 0 convert nullptr):

template<typename tag> bool operator>(const distance<tag>& a, std::nullptr_t b) {return a.value > 0;}

c++ templates c++11

No comments:

Post a Comment