繁体   English   中英

模板部分专业化

[英]Template partial specialization

template<class T>
struct TypeX;

template<>
struct TypeX<int(...)>//HERE IF WITHOUT ELLIPSIS IT WILL COMPILE
{
    static std::string get_type()
    {
        return "int()";
    }
};

template<>
struct TypeX<int>
{
    static std::string get_type()
    {
        return "int";
    }
};

template<class T>
struct type_descriptor
{
    typedef T type;
    typedef typename std::remove_reference<T>::type no_ref_type;
    typedef typename std::remove_pointer<no_ref_type>::type no_ref_no_pointer_type;
    typedef typename std::remove_cv<no_ref_no_pointer_type>::type no_ref_no_pointer_no_cv_type;
    typedef typename std::remove_all_extents<no_ref_no_pointer_no_cv_type>::type no_ref_no_pointer_no_cv_no_ext_type;
    typedef no_ref_no_pointer_no_cv_no_ext_type bare_type;

    enum {isArray = std::is_array<T>::value, isPointer = std::is_pointer<T>::value, isRef = std::is_reference<T>::value};

    static std::string get_type()
    {
        return pointer_<isPointer>() + array_<std::is_array<no_ref_no_pointer_type>::value>() + TypeX<bare_type>::get_type();

    }
};
template<bool C>
std::string array_()
{return "";}

template<>
std::string array_<true>()
{return "array of";}

template<bool C>
std::string pointer_()
{return "";}

template<>
std::string pointer_<true>()
{return "pointer to";}

int _tmain(int argc, _TCHAR* argv[])
{
    cout << type_descriptor<int(*)()>::get_type();

    return 0;
}

请查看代码中的注释。 问题是为什么我是否专门研究省略号(假设任何数字我都会出错),但是当我专门研究无参数时,它会编译吗?

问题是为什么我是否专门研究省略号(假设任何数字我都会出错),但是当我专门研究无参数时,它会编译吗?

因为省略号并不意味着任何括号(因为您试图在main使用它)。 省略号用于表示函数(C ++ 03)中可变数量的参数。


编辑:也许下面的示例为您提供了足够的提示来实现您想要的:

template<class T>
struct TypeX
{
        TypeX() { cout << "TypeX" << endl; }
};

template<typename T>
struct TypeX<T(*)()> //will match with : int (*)(), char (*)(), etc!
{
        TypeX() { cout << "TypeX<T(*)()>" << endl; }
};

template<typename T, typename S>
struct TypeX<T(*)(S)> //will match with : int (*)(int), char (*)(int), etc!
{
        TypeX() { cout << "TypeX<T(*)(S)>" << endl; }
};

template<typename T, typename S, typename U>
struct TypeX<T(*)(S, U)> //will match with : int (*)(int, char), char (*)(int, int), etc!
{
       TypeX() { cout << "TypeX<T(*)(S, U)>" << endl; }
};
int main() {
        TypeX<int*>();
        TypeX<int(*)()>();
        TypeX<int(*)(int)>();
        TypeX<int(*)(char)>();
        TypeX<int(*)(char, int)>();
        TypeX<int(*)(short, char)>();
        return 0;
}

输出:

TypeX
TypeX<T(*)()>
TypeX<T(*)(S)>
TypeX<T(*)(S)>
TypeX<T(*)(S, U)>
TypeX<T(*)(S, U)>

ideone上的演示: http : //www.ideone.com/fKxKK

省略号表示函数在每个调用位置可以接受任意数量的参数。

int f(...);  // signature

int x = f();   // invocations
int y = f(my_int, my_double);

函数签名本身每次调用天真的隐含的更具体的签名(即int f()int f(int, double) )不同。

因此,尽管您可以专门处理int (*)(...)情况,但只有实际指定初始省略号的函数类型将匹配。 其他函数,例如int (*)(int)不匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM