模板之间的隐式转换
Implicit conversion between templates
我不太明白为什么这段代码不能编译。
应该可以像这样调用 dist() :
dist(GenericVec2<T>,GenericVec3<T>)
(不管这有多可怕)。这个想法是 GenericVec3 参数被转换运算符隐式转换为 GenericVec2 。
我在这里找到了这个问题
C++ implicit type conversion with template
,但我不太确定它是否可以应用于我的问题(将转换运算符设置为“friend
”无效)。 VS 输出以下错误:
error C2672: 'dist': no matching overloaded function found
error C2784: 'F dist(const GenericVec2<F> &,const GenericVec2<F> &)': could not deduce template argument for 'const GenericVec2<F> &' from 'Vec3'
note: see declaration of 'dist'
这是我的代码:
#include <iostream>
template<typename F> struct GenericVec2
{
GenericVec2<F>::GenericVec2(F _x = 0, F _y = 0) : x(_x), y(_y) {}
F x;
F y;
};
using Vec2 = GenericVec2<float>;
template<typename F> struct GenericVec3
{
GenericVec3<F>::GenericVec3(F _x = 0, F _y = 0, F _z = 0) : x(_x), y(_y), z(_z) {}
operator GenericVec2<F>() { return *reinterpret_cast<GenericVec2<F>*>(&x); }
operator const GenericVec2<F>() const { return *reinterpret_cast<const GenericVec2<F>*>(&x); }
F x;
F y;
F z;
};
using Vec3 = GenericVec3<float>;
template<typename F> F dist(const GenericVec2<F>& a, const GenericVec2<F>& b)
{
return std::hypot(a.x - b.x, a.y - b.y);
}
int main()
{
Vec2 a{ 2.0f, 3.0f };
Vec3 b{ 1.0f, 1.0f, 1.0f };
Vec2 c = b;
float d = dist(a, Vec2{ b }); // works
float e = dist(a, b); // doesn't compile
std::cin.ignore();
return 0;
}
提前致谢!
-托马斯
为了使隐式转换成为可能,目标 class 应具有一个构造函数,该构造函数接收源类型(或可从源类型转换的类型)作为参数。 GenericVec3
中定义的转换运算符用于显式转换操作。
所以你应该在你的 GenericVec2
class
中定义以下构造函数
GenericVec2(const GenericVec3<F>& v3) { ... }
问题是,在
template<typename F> F dist(const GenericVec2<F>& a, const GenericVec2<F>& b)
你不能从 GenericVec2<float>
推导出第二个参数。
由于 friend
:
,一种解决方案是使函数成为非模板
template<typename F> struct GenericVec2
{
GenericVec2<F>::GenericVec2(F _x = 0, F _y = 0) : x(_x), y(_y) {}
friend F dist(const GenericVec2& a, const GenericVec2& b)
{
return std::hypot(a.x - b.x, a.y - b.y);
}
F x;
F y;
};
我不太明白为什么这段代码不能编译。 应该可以像这样调用 dist() :
dist(GenericVec2<T>,GenericVec3<T>)
(不管这有多可怕)。这个想法是 GenericVec3 参数被转换运算符隐式转换为 GenericVec2 。 我在这里找到了这个问题
C++ implicit type conversion with template
,但我不太确定它是否可以应用于我的问题(将转换运算符设置为“friend
”无效)。 VS 输出以下错误:
error C2672: 'dist': no matching overloaded function found
error C2784: 'F dist(const GenericVec2<F> &,const GenericVec2<F> &)': could not deduce template argument for 'const GenericVec2<F> &' from 'Vec3'
note: see declaration of 'dist'
这是我的代码:
#include <iostream>
template<typename F> struct GenericVec2
{
GenericVec2<F>::GenericVec2(F _x = 0, F _y = 0) : x(_x), y(_y) {}
F x;
F y;
};
using Vec2 = GenericVec2<float>;
template<typename F> struct GenericVec3
{
GenericVec3<F>::GenericVec3(F _x = 0, F _y = 0, F _z = 0) : x(_x), y(_y), z(_z) {}
operator GenericVec2<F>() { return *reinterpret_cast<GenericVec2<F>*>(&x); }
operator const GenericVec2<F>() const { return *reinterpret_cast<const GenericVec2<F>*>(&x); }
F x;
F y;
F z;
};
using Vec3 = GenericVec3<float>;
template<typename F> F dist(const GenericVec2<F>& a, const GenericVec2<F>& b)
{
return std::hypot(a.x - b.x, a.y - b.y);
}
int main()
{
Vec2 a{ 2.0f, 3.0f };
Vec3 b{ 1.0f, 1.0f, 1.0f };
Vec2 c = b;
float d = dist(a, Vec2{ b }); // works
float e = dist(a, b); // doesn't compile
std::cin.ignore();
return 0;
}
提前致谢!
-托马斯
为了使隐式转换成为可能,目标 class 应具有一个构造函数,该构造函数接收源类型(或可从源类型转换的类型)作为参数。 GenericVec3
中定义的转换运算符用于显式转换操作。
所以你应该在你的 GenericVec2
class
GenericVec2(const GenericVec3<F>& v3) { ... }
问题是,在
template<typename F> F dist(const GenericVec2<F>& a, const GenericVec2<F>& b)
你不能从 GenericVec2<float>
推导出第二个参数。
由于 friend
:
template<typename F> struct GenericVec2
{
GenericVec2<F>::GenericVec2(F _x = 0, F _y = 0) : x(_x), y(_y) {}
friend F dist(const GenericVec2& a, const GenericVec2& b)
{
return std::hypot(a.x - b.x, a.y - b.y);
}
F x;
F y;
};