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

C++ C++;泛型编程中的类型验证,c++,generics,typeid,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

其目的是编写一个通用模板函数,该函数可以计算两点之间的距离(例如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);
所以我的代码是这样的:

#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));
}
#包括
#包括
#包括
#包括
#包括“hopp/vector2.hpp”
#包括“Qt/qpoint.h”
#包括“SFML/Vector2.hpp”
#包括“wxWidgets/gdicmn.h”
模板自动距离2(T1 p1、T2 p2)
{
自动x1=0.0;
自动y1=0.0;
自动x2=0.0;
自动y2=0.0;
/*
*如果p1是一个类。
*/
if(typeid(p1).name()==typeid(Point).name()||
typeid(p1).name()==typeid(Point).name()||
typeid(p1).name()==typeid(Point).name()||
typeid(p1).name()==typeid(hopp::vector2).name()||
typeid(p1).name()==typeid(sf::Vector2).name()||
typeid(p1).name()==typeid(wxRealPoint).name()||
typeid(p1).name()==typeid(QPointF).name()
) {
x1=p1.x;
y1=p1.y;
}
/*
*如果p1是数组或向量。
*/
else if(typeid(p1).name()==typeid(std::array).name()
||
typeid(p1).name()==typeid(std::vector).name()||
typeid(p1).name()==typeid(std::array).name()||
typeid(p1).name()==typeid(std::vector).name()||
typeid(p1).name()==typeid(std::array).name()||
typeid(p1).name()==typeid(std::vector).name()
){
x1=p1[0];
y1=p1[1];
}
if(typeid(p2).name()==typeid(Point).name()||
typeid(p2).name()==typeid(Point).name()||
typeid(p2).name()==typeid(Point).name()||
typeid(p2).name()==typeid(hopp::vector2).name()||
typeid(p2).name()==typeid(sf::Vector2).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).name()
||
typeid(p2).name()==typeid(std::vector).name()||
typeid(p2).name()==typeid(std::array).name()||
typeid(p2).name()==typeid(std::vector).name()||
typeid(p2).name()==typeid(std::array).name()||
typeid(p2).name()==typeid(std::vector).name()
){
x2=p2[0];
y2=p2[1];
}
自动差异x=x1-x2;
自动微分y=y1-y2;
返回sqrt(pow(diff_x,2)+pow(diff_y,2));
}

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

您应该能够将模板函数与重载函数结合起来提取x和y值:

template<typename T1,typename T2>
auto distance(T1 p1, T2 p2)
{
  const auto x1 = getX(p1);
  const auto y1 = getY(p1);
  const auto x2 = getX(p2);
  const auto y2 = getY(p2);

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

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

auto getX(const std::vector<double>& v)
{
  return v[0];
}

auto getX(const Point<double>& v)
{
  return v.x;
}

...
模板
自动距离(T1 p1、T2 p2)
{
常数自动x1=getX(p1);
常数自动y1=getY(p1);
const auto x2=getX(p2);
const auto y2=getY(p2);
常数自动差x=x1-x2;
常数自动微分y=y1-y2;
返回sqrt(pow(diff_x,2)+pow(diff_y,2));
}
自动获取(const std::vector&v)
{
返回v[0];
}
自动获取X(常数点和v)
{
返回v.x;
}
...
要减少重载函数的数量,可以让getter返回std::tuple:

template<typename T1,typename T2>
auto distance(T1 p1, T2 p2)
{
  const auto p1_pair = get_values(p1);
  const auto p2_pair = get_values(p2);

  const auto diff_x = std::get<0>(p1_pair) - std::get<0>(p2_pair);
  const auto diff_y = std::get<1>(p1_pair) - std::get<1>(p2_pair);

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

auto get_values(const std::vector<double>& v)
{
  return std::make_tuple(v[0], v[1]);
}

auto get_values(const Point<double>& v)
{
  return std::make_tuple(v.x, v.y);
}

...
模板
自动距离(T1 p1、T2 p2)
{
常量自动p1_对=获取_值(p1);
常量自动p2_对=获取_值(p2);
const auto diff_x=std::get(p1_对)-std::get(p2_对);
const auto diff_y=std::get(p1_对)-std::get(p2_对);
返回sqrt(pow(diff_x,2)+pow(diff_y,2));
}
自动获取_值(const std::vector&v)
{
返回std::make_tuple(v[0],v[1]);
}
自动获取_值(常量点和v)
{
返回std::make_tuple(v.x,v.y);
}
...

为避免大量过载,请使用sfinae机制,例如如下():

#包括
#包括
#包括
#包括
结构点{
双x;
双y;
};
模板
自动获取x(T)->decltype(T.x){
返回t.x;
}
模板
自动获取x(T)->decltype(T[0]){
返回t[0];
}
模板
自动getY(T)->decltype(T.y){
返回t.y;
}
模板
自动getY(T)->decltype(T[1]){
返回t[1];
}
模板
自动距离(T1 p1、T2 p2){
自动x1=getX(p1);
自动x2=getX(p2);
自动y1=getY(p1);
自动y2=getY(p2);
自动差异x=x1-x2;
自动微分y=y1-y2;
返回sqrt(pow(diff_x,2)+pow(diff_y,2));
}
int main(){
p1点;
向量p2={1,2};

std::你为什么不能重载一个模板函数呢?我个人会编写一个
get_points
函数。它重载了你想要支持的所有类型,你可以处理如何从函数中的类型提取数据的逻辑。然后在泛型函数中,你只需调用get points来获取数据,然后进行计算距离。非常感谢你的回答。我已经测试了代码,当x和y在某个点上是公共的时,它运行得很好。当x和y是私有的时,只需使用decltype(t.getPrivateX())添加两个函数即可获得它们。
template<typename T1,typename T2>
auto distance(T1 p1, T2 p2)
{
  const auto p1_pair = get_values(p1);
  const auto p2_pair = get_values(p2);

  const auto diff_x = std::get<0>(p1_pair) - std::get<0>(p2_pair);
  const auto diff_y = std::get<1>(p1_pair) - std::get<1>(p2_pair);

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

auto get_values(const std::vector<double>& v)
{
  return std::make_tuple(v[0], v[1]);
}

auto get_values(const Point<double>& v)
{
  return std::make_tuple(v.x, v.y);
}

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