Search code examples
c++genericstypeid

C++ type verification in generic programming


The aim is to write a generic template function who can calculate the distance between two points(take for example p1 and p2 as two parameters). The point can be represented in many ways:

hopp::vector2<double> p0(0.0, 0.0);
sf::Vector2<double> p1(0.0, 1.0);
std::array<double, 2> p2 = { 1.0, 1.0 };
std::vector<double> p3 = { 1.0, 0.0 };
wxRealPoint p4(1.0, -1.0);
QPointF p5(0.0, -1.0);

And the function should be like :

distance(p0,p1)
distance(p1,p2)
....

so my code goes like:

#include <iostream>
#include <math.h>
#include <array>
#include <vector>
#include "hopp/vector2.hpp"
#include "Qt/qpoint.h"
#include "SFML/Vector2.hpp"
#include "wxWidgets/gdicmn.h"
template<class T1,class T2> auto distance2(T1 p1, T2 p2)
{
   auto x1 = 0.0;
   auto y1 = 0.0;
   auto x2 = 0.0;
   auto y2 = 0.0;
  /*
   * if p1 is a class.
   */


  if (typeid(p1).name() == typeid(Point<int>).name() ||
      typeid(p1).name() == typeid(Point<double>).name()||
      typeid(p1).name() == typeid(Point<float>).name() ||
      typeid(p1).name() == typeid(hopp::vector2<double>).name() ||
      typeid(p1).name() == typeid(sf::Vector2<double>).name() ||
      typeid(p1).name() == typeid(wxRealPoint).name() ||
      typeid(p1).name() == typeid(QPointF).name()
    ) {
       x1 = p1.x;
       y1 = p1.y;
  }
  /*
  * if p1 is a array or vector.
  */
  else if(   typeid(p1).name() == typeid(std::array<double, 2>).name()
   ||
             typeid(p1).name() == typeid(std::vector<double>).name() ||
             typeid(p1).name() == typeid(std::array<int>).name() ||
             typeid(p1).name() == typeid(std::vector<int>).name() ||
             typeid(p1).name() == typeid(std::array<float>).name() ||
             typeid(p1).name() == typeid(std::vector<float>).name()

           ){
      x1 = p1[0];
      y1 = p1[1];
  }

  if (  typeid(p2).name() == typeid(Point<int>).name() ||
        typeid(p2).name() == typeid(Point<double>).name()||
        typeid(p2).name() == typeid(Point<float>).name() ||
        typeid(p2).name() == typeid(hopp::vector2<double>).name() ||
        typeid(p2).name() == typeid(sf::Vector2<double>).name() ||
        typeid(p2).name() == typeid(wxRealPoint).name() ||
        typeid(p2).name() == typeid(QPointF).name()
  )
  {
     x2 = p2.x;
     y2 = p2.y;
  } else if (typeid(p2).name() == typeid(std::array<double, 2>).name()
       ||
             typeid(p2).name() == typeid(std::vector<double>).name() ||
             typeid(p2).name() == typeid(std::array<int>).name() ||
             typeid(p2).name() == typeid(std::vector<int>).name() ||
             typeid(p2).name() == typeid(std::array<float>).name() ||
             typeid(p2).name() == typeid(std::vector<float>).name()

         ){
       x2 = p2[0];
       y2 = p2[1];
  }


  auto diff_x = x1-x2;
  auto diff_y = y1-y2;

  return sqrt(pow(diff_x,2)+pow(diff_y,2));
}

There are many errors when compiling and I don't think it's a good proposition to do many type verification using 'typeid'. How should I deal with this problem?


Solution

  • To avoid tons of overloads use sfinae mechanism e.g. as follows (live demo):

    #include <iostream>
    #include <math.h>
    #include <array>
    #include <vector>
    
    struct Point {
        double x;
        double y;
    };
    
    template <class T>
    auto getX(T t) -> decltype(t.x) {
        return t.x;
    }
    
    template <class T>
    auto getX(T t) -> decltype(t[0]) {
        return t[0];
    }
    
    template <class T>
    auto getY(T t) -> decltype(t.y) {
        return t.y;
    }
    
    template <class T>
    auto getY(T t) -> decltype(t[1]) {
        return t[1];
    }
    
    template <class T1, class T2>
    auto distance(T1 p1, T2 p2) {
        auto x1 = getX(p1);
        auto x2 = getX(p2);
        auto y1 = getY(p1);
        auto y2 = getY(p2);
        auto diff_x = x1-x2;
        auto diff_y = y1-y2;
    
        return sqrt(pow(diff_x,2)+pow(diff_y,2));
    }
    
    
    
    int main() {
        Point p1;
        std::vector<double> p2 = {1, 2};
        std::cout << distance(p1, p2) << std::endl;
    }
    

    This should work independently of the point type as long as the type does not have x member and at the same time overloaded operator[].