c++ - 如何部分特化模板 C++ 类?

标签 c++ templates c++11 partial-specialization

我写了一个像这样的通用矩阵类:

template <std::size_t N, std::size_t M, class T>
class CMatrix
{
protected:
    T* m_elem;
public:
    CMatrix() : m_elem(new T[N*M]) {}
    ~CMatrix() { delete[] m_elem; }
    inline bCMatrix& operator*=(const T& val);
    inline bCMatrix& operator/=(const T& val);
    ...
    template <std::size_t L> inline CMatrix<L, N, T> operator*(const CMatrix<M, L, T>& mat) const;
};

现在我想为 CMatrix<2,2,T> 做几个特化。 我想添加一个 comstructor CMatrix<2,2,T>(const T& a00, const T& a01, const T& a10, const T& a11) {...} . 我喜欢专门针对 CMatrix<2x2>*CMatrix<2x2> 的矩阵乘法。但另一方面,我想保留 *= 和/= 等的功能。

我该怎么做?

我唯一的工作尝试是重写(复制/粘贴/搜索/替换)特化 N=3、M=3 的所有代码,例如:

template <class T>
class CMatrix<3,3,T>
{
...
};

现在我可以专门针对 2x2 进行矩阵-矩阵乘法运算。但是我有两段几乎相同的代码,例如一段用于通用矩阵 vector 乘法,另一段用于矩阵 2x2 的矩阵 vector 乘法。如果我优化通用的,则两者都必须更改。很难维持这一点。

最佳答案

你可以阅读一些关于类模板的文档,比如http://en.cppreference.com/w/cpp/language/partial_specialization .对您而言,它需要以如下方式定义特化:

template<typename T>
class CMatrix<2, 2, T>
{
public:
    CMatrix<2,2,T>(const T& a00, const T& a01, const T& a10, const T& a11);
    ...
    template <std::size_t L> inline CMatrix<L, 2, T> operator*(const CMatrix<2, L, T>& mat) const;

    // Special overload for 2x2 x 2x2 multiplication
    inline CMatrix<2, 2, T> operator*(const CMatrix<2, 2, T>& mat) const;
};

为了防止 operator*= 的代码重复等你可以做一个template<int M, int N, typename T> class CMatrixBase {}并在那里实现它们,并让您的实际矩阵类和特化从中继承。

Martin 建议使用 CRTP,它可以帮助您避免丢失类型信息。这看起来像这样:

template<int M, int N, typename T, typename Derived>
class CMatrixBase
{
public:
    Derived& operator*=(T val) {
        doMultiplication(val);
        return *static_cast<Derived*>(this);
    }
};

关于c++ - 如何部分特化模板 C++ 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35921093/

相关文章:

c++ - 如何使用通用函数指针作为模板参数?

php - 如何在 CodeIgniter 中加载页眉、侧边栏、页脚等页面 View ?

python - Python 中的字符串模板 : what are legal characters?

pointers - 错误 : forming pointer to reference type 'const std::pair<double, unsigned int>&' . ...我无法理解这个错误

c++ - 如何释放数组的 N 个元素的内存

c++ - QMutex 与 QThread - 段错误

c++ - 关于C++类定义的问题 ": 1"

c++ - unique_ptr 和 shared_ptr 的奇怪段错误

c++ - 在 C++ 中将 unsetf 与十六进制十进制数和八进制数一起使用

c++ - 在 C++ 中使用命令行参数的 cgi 脚本