您能否实例化模板的非专用版本并在专用化中继承它?

Can you instantiate the unspecialized version of a template and inherit from it inside the specialization?

本文关键字:专用 继承 版本 实例化      更新时间:2023-10-16

我正在尝试将Vector定义为行/列MatrixVector需要一些Matrix没有的方法,所以我专门Matrix

template<typename T, unsigned N, unsigned M>
struct Matrix {
T data[N][M];
};
template <typename T, unsigned N>
struct Matrix<T, N, 1> : public Matrix<T, N, 1> {
T at(unsigned index) {
return data[index][0];
}
};
template <typename T, unsigned N>
using Vector = Matrix<T, N, 1>;

此代码不编译,因为专用化是递归类型。我想要在这里继承的原因是,我可以将Matrix的所有内容都包含在专业化中,而无需复制和粘贴。

有没有办法实例化原始Matrix并从中继承?如果有,我的类型会变得非递归吗?

我想到的解决这个问题的另一种方法是简单地将常规Matrix的内容#include到初始定义和所有专业化中。但这远非惯用语。

这里有一种方法可以做到这一点:

template<typename T, unsigned N, unsigned M>
struct Matrix {
T data[N][M];
};
template<typename T, unsigned N>
struct MatrixTN : Matrix <T, N, 1> {
T at(unsigned index) {
return this->data[index][0];
}
};
template<typename T, unsigned N>
using Vector = MatrixTN <T, N>;

请注意,需要this->data才能将data评估延迟到模板查找的第二阶段。

现场演示

在 C++20 中,您可以使用requires来丢弃方法:

template<typename T, unsigned N, unsigned M>
struct Matrix {
T data[N][M];
T at(unsigned index) requires (M == 1) {
return data[index][0];
}
};

在以前的标准中,您可以改用 SFINAE。

您可以使用 SFINAE 禁用方法。必须具有模板方法,其中包含依赖于类模板参数的模板参数。

C++11:

#include <type_traits>
template<typename T, unsigned N, unsigned M=1>
struct Matrix 
{
T data[N][M];
template<typename V = T>
typename std::enable_if<M == 1, V>::type at(unsigned index)
{
return data[index][0];
}
};
template <typename T, unsigned N>
using Vector = Matrix<T, N, 1>;
int main()
{
Matrix<int, 2, 3> m;
Vector<int, 5> v;
// m.at(0); 
v.at(1);
}

C++14:

#include <type_traits>
template<typename T, unsigned N, unsigned M=1>
struct Matrix 
{
T data[N][M];
template<typename V = T>
std::enable_if_t<M == 1, V> at(unsigned index)
{
return data[index][0];
}
};
template <typename T, unsigned N>
using Vector = Matrix<T, N, 1>;
int main()
{
Matrix<int, 2, 3> m;
Vector<int, 5> v;
// m.at(0); 
v.at(1);
}

C++20(感谢Jarod42(:

template<typename T, unsigned N, unsigned M = 1>
struct Matrix 
{
T data[N][M];
T at(unsigned index) requires (M == 1)
{
return data[index][0];
}
};
template <typename T, unsigned N>
using Vector = Matrix<T, N, 1>;
int main()
{
Matrix<int, 2, 3> m;
Vector<int, 5> v;
// m.at(0); 
v.at(1);
}

戈德博尔特