我有一个带有 int 元素的向量和一个带有 float 元素的向量。在这种情况下,最喜欢的类型是浮点数,在点积中我想返回浮点数类型(如果未指定)。
下面的代码就是这样做的,但是模板包出了点问题。
我能得到一点帮助吗?
#include <iostream>
#include <vector>
#include <array>
using namespace std;
template<typename Tp, typename T1> struct contains
{
constexpr static bool value = std::is_same<Tp, T1>::value;
};
template<typename Tp, typename T1, typename ... Tn> struct contains
{
constexpr static bool value = std::is_same<Tp, T1>::value || contains<Tp, Tn...>::value;
};
template<typename ... T> struct perfect
{
typedef
typename std::conditional<contains<long double, T...>::value, long double,
typename std::conditional<contains<double , T...>::value, double,
typename std::conditional<contains<float , T...>::value, float,
typename std::conditional<contains<long long , T...>::value, long long,
typename std::conditional<contains<unsigned long long, T...>::value, unsigned long long,
typename std::conditional<contains<int , T...>::value, int,
typename std::conditional<contains<unsigned int, T...>::value, unsigned int,
//.....
void
>>>>>>>::type type;
};
struct Vector1 : public std::vector<double> {};
struct Vector2 : public std::vector<int> {};
struct Vector3 : public std::vector<float> {};
template<typename C1, typename C2, typename T = perfect<typename C1::value_type, typename C2::value_type>::type>
T dot_product(const C1 &c1, const C2 &c2)
{
return 0; // return c1 * c2
}
int main()
{
Vector1 a;
Vector2 b;
cout << dot_product(a, b) << endl;
cout << dot_product<Vector1, Vector2, long double>(a, b) << endl;
}