목적은 두 점 사이의 거리를 계산할 수있는 일반 템플릿 기능을 작성하는 것입니다 (예 : p1과 p2를 두 개의 매개 변수로 사용).일반 프로그래밍의 C++ 유형 확인
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'를 사용하여 많은 타입 검증을하는 것이 좋은 제안이라고 생각하지 않는다. 이 문제를 어떻게 처리해야합니까?
왜 템플릿 기능을 사용하면 과부하가 걸리는 이유는 무엇입니까? – Borgleader
개인적으로'get_points' 함수를 작성했습니다. 지원하려는 모든 유형에 대해 오버로드되며 함수의 유형에서 데이터를 추출하는 방법에 대한 논리를 처리합니다. 그런 다음 일반 함수에서 점을 가져 와서 데이터를 얻은 다음 거리를 계산합니다. – NathanOliver