【发布时间】:2021-12-14 19:10:18
【问题描述】:
我有一个基于模板的 Vector 类。 我有一个基于Scott Meyers Dimensional Analysis in C++ 的单位转换模板。我不会列出它,因为它很复杂,除非没有这些细节就无法解决问题。当我需要将数据传递给外部函数时,我正在使用强制转换运算符。
template<typename T>
struct XYZVector
{
operator Point3D() { return Point3D(x, y, z); }
}
如果我使用 T 作为双精度,它工作正常。但是当我这样做时,我需要专业化。
Point3D vecOut = XYZVector<Unit::usFoot>(x,y,z);
会失败,因为在转换 Unit 对象时需要使用 as() 方法。
operator Point3D() { return Point3D( x.as<T>(), y.as<T>(), z.as<T>() ); }
所以我需要以某种方式说如果 T 是一个 Unit 使用这一行,否则使用下一行。
template<typename T>
struct XYZVector
{
//if typename T is a Unit type use this
operator Point3D() { return Point3D( x.as<T>(), y.as<T>(), z.as<T>() ) }
//otherwise use this
operator Point3D() { return Point3D(x, y, z); }
}
这可能吗?
【问题讨论】:
标签: c++ templates visual-c++ c++17 c++14