泛型编程中的 C++ 类型验证

标签 c++ generics typeid

目标是编写一个通用模板函数,可以计算两点之间的距离(例如,以 p1 和 p2 作为两个参数)。该点可以用多种方式表示:

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);

函数应该是这样的:

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

所以我的代码如下:

#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));
}

编译时有很多错误,我认为使用“typeid”进行多次类型验证不是一个好建议。我该如何处理这个问题?

最佳答案

为了避免大量过载,请使用 sfinae 机制,例如如下(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;
}

只要类型没有 x 成员并且同时重载 operator[],这应该独立于点类型工作。

关于泛型编程中的 C++ 类型验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41128457/

相关文章:

c++ - 如何访问位于 mdi 子窗口中的 TextEdit 的文本属性?

c++ - 为某些类禁用 RTTI

当我们试图找到 2 个大值 1.18973e+4932 的方差和均值时,C++ 计算 inf

c++ - Gtk 在它的回调上隐藏窗口

typescript 泛型类作为函数参数

编译时的 C++ 类型标识

c++ - 预期等价物 : typeid of data expression and type expression

c++ - 有没有办法使用 RTTI 获取基于签名的类型信息损坏的函数名称?

delphi - Delphi 中如何将泛型类型转换为实际类型

c# - 如何为组件/脚本创建泛型池系统?