[英]Syntax for partial class template specialization
在下面的内容中,我是否忘记了对部分专业化类NumInfo的某些正确语法,或者甚至有可能这样做?
template<typename T>
struct NumInfo {
T x;
T y;
void Print();
};
template<typename T>
void NumInfo <T>::Print() {
/*.....*/
}
template<typename T>
struct NumInfo <float> {
T x;
float y;
void Print();
};
template<typename T>
void NumInfo <float>::Print() {
/*.....*/
}
您的设计有问题-现在您有多个类,它们具有相同的名称NumInfo<float>
和不同的定义(取决于T
)。 要解决此问题,您需要另一个模板参数,如下所示:
template<typename S, typename T=S>
struct NumInfo
{
T x;
S y;
void Print();
};
template<typename S, typename T>
void NumInfo<S,T>::Print()
{
/*.....*/
}
template<typename T>
struct NumInfo<float,T>
{
T x;
float y;
void Print();
};
template<typename T>
void NumInfo<float,T>::Print()
{
/*.....*/
}
template<typename T>
struct NumInfo {
T x;
T y;
void Print();
};
template<typename T>
void NumInfo <T>::Print() {
/*.....*/
}
template<>
struct NumInfo <float> {
typedef float T;
T x;
float y;
void Print();
};
void NumInfo <float>::Print() {
/*.....*/
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.