部分类模板专门化的语法 [英] Syntax for partial class template specialization

查看:137
本文介绍了部分类模板专门化的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的内容中,我忘记了部分专业化类NumInfo的一些正确的语法,还是甚至可以这样做?

In the following, am I forgetting some correct syntax for partial specializing class NumInfo or is it even possible to do that?

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 )。为了解决这个问题,你需要一个第二个模板参数,例如:

Your design has a problem -- right now you have multiple classes with the same name NumInfo<float> and different definitions (depending on T). To fix that, you'll need a second template parameter, like this:

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()
{
    /*.....*/
}

这篇关于部分类模板专门化的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆