C++部分模板实例化中的 typedef
C++ Typedef in Partial Template Instantiation
我无法弄清楚为什么我的部分实例化模板没有在C++中编译。
我正在尝试提供一个类,该类根据传递给它的模板返回不同的类型:
enum EVectorType {
eVectorType_Scalar,
eVectorType_Vector,
eVectorType_Matrix
};
template<
EVectorType kVectorTypeOne,
EVectorType kVectorTypeTwo,
typename TypeOne,
typename TypeTwo>
class MultSwitch {
private:
const TypeOne &m_A;
const TypeTwo &m_B;
public:
typedef TypeOne ResultType;
MultSwitch(const TypeOne &a, const TypeTwo &b)
: m_A(a), m_B(b) { }
ResultType GetMultiplication() const { return m_A * m_B; }
};
该类用作*
运算符的重载:
template<typename T>
class VectorTraits {
public:
static const EVectorType kVectorType = eVectorType_Scalar;
};
template<typename T, const int N>
class VectorTraits<VectorBase<T, N> > {
public:
static const EVectorType kVectorType = eVectorType_Vector;
};
template<typename TypeOne, typename TypeTwo>
static inline
typename MultSwitch<
VectorTraits<TypeOne>::kVectorType,
VectorTraits<TypeTwo>::kVectorType,
TypeOne, TypeTwo
>::ResultType
operator*(const TypeOne &v1, const TypeTwo &v2) {
typedef MultSwitch<
VectorTraits<TypeOne>::kVectorType,
VectorTraits<TypeTwo>::kVectorType,
TypeOne, TypeTwo
> VSwitch;
return VSwitch(v1, v2).GetMultiplication();
}
以下专业按预期工作:
template<typename TypeOne, typename TypeTwo>
class MultSwitch<
eVectorType_Scalar,
eVectorType_Vector,
TypeOne, TypeTwo> {
private:
const TypeOne &m_A;
const TypeTwo &m_B;
public:
typedef TypeTwo ResultType;
MultSwitch(const TypeOne &a, const TypeTwo &b)
: m_A(a), m_B(b) { }
ResultType GetMultiplication() const { return ScalarMultiply(m_B, m_A); }
};
template<typename TypeOne, typename TypeTwo>
class MultSwitch<
eVectorType_Vector,
eVectorType_Scalar,
TypeOne, TypeTwo> {
private:
const TypeOne &m_A;
const TypeTwo &m_B;
public:
typedef TypeOne ResultType;
MultSwitch(const TypeOne &a, const TypeTwo &b)
: m_A(a), m_B(b) { }
ResultType GetMultiplication() const { return ScalarMultiply(m_A, m_B); }
};
template<typename TypeOne, typename TypeTwo>
class MultSwitch<
eVectorType_Vector,
eVectorType_Vector,
TypeOne, TypeTwo> {
private:
const TypeOne &m_A;
const TypeTwo &m_B;
public:
typedef typename TypeOne::ScalarType ResultType;
MultSwitch(const TypeOne &a, const TypeTwo &b)
: m_A(a), m_B(b) { }
ResultType GetMultiplication() const { return m_A.Dot(m_B); }
};
但是,以下内容不会:
template<typename TypeOne, typename TypeTwo>
class MultSwitch<
eVectorType_Matrix,
eVectorType_Matrix,
TypeOne, TypeTwo> {
private:
const TypeOne &m_A;
const TypeTwo &m_B;
public:
typedef MatrixBase<typename TypeOne::ScalarType, TypeOne::kNumRows, TypeTwo::kNumCols> ResultType;
MultSwitch(const TypeOne &a, const TypeTwo &b)
: m_A(a), m_B(b) { }
ResultType GetMultiplication() const { return m_A.MultiplyMatrix(m_B); }
};
每当我写一个表达式a * b
a
和b
的类型特征匹配eVectorType_Matrix
,出于某种原因,编译器说 ResultType 与 TypeOne
或 a
的类型相同。我已经更改了工作专业化中的各种不同类型定义,但它们似乎都产生了相同的错误。
更具体地说,以下代码:
template <typename T, const int nRows, const int nCols>
class MatrixBase {
public:
typedef T ScalarType;
static const int kNumRows = nRows;
static const int kNumCols = nRows;
...
};
template<typename T, const int N, const int M>
class VectorTraits<MatrixBase<T, N, M> > {
public:
static const EVectorType kVectorType = eVectorType_Matrix;
};
MatrixBase<int, 2, 3> a;
...
MatrixBase<int, 3, 5> b;
...
MatrixBase<float, 2, 5> amb = a * b;
生成以下错误:
TestMatrix.cpp:145:42: error: conversion from ‘MultSwitch<(EVectorType)2u, (EVectorType)2u, MatrixBase<int, 2, 3>, MatrixBase<int, 3, 5> >::ResultType {aka MatrixBase<int, 2, 3>}’ to non-scalar type ‘MatrixBase<float, 2, 5>’ requested
MatrixBase<float, 2, 5> amb = a * b;
我相信你有两个问题:
-
在MatrixBase中,我认为
static const int kNumCols = nRows;
应该是
static const int kNumCols = nCols;
-
"a * b" 将返回
MatrixBase<int, 2, 5>
类型,而不是
MatrixBase<float, 2, 5>
需要添加复制构造函数来执行 int->float 转换。
相关文章:
- 从C++实例化QML
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 约束和显式模板实例化
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 对象实例化调用构造函数的次数太多
- 如何使用非默认构造函数实例化模板化类
- 静态数据成员模板专用化的实例化点在哪里
- 错误的cv::face FacemarkLBF实例化
- C++的解析器在可以区分比较和模板实例化之前会做什么?
- 为什么 gcc 和 clang 为函数模板的实例化生成不同的符号名称?
- 通过 typedef 模板<类型名 T、T> 强制模板实例化 - 为什么有效?
- 进行Typedef并使用模板实例化
- typedef 中的模板实例化的范围是否有限
- C++部分模板实例化中的 typedef
- 通过typedef显式实例化模板
- typedef和显式实例化之间的代码重复
- 在模板实例化和extern模板声明中使用typedef
- 使用宏是为了为可变模板实例化提供typedef,这是使用宏的正当理由