【发布时间】:2011-04-17 16:42:44
【问题描述】:
可能很容易解决,但很难找到解决方案:
是否可以(部分)专门化一整套类型? 在示例中,“Foo”应部分专门用于 (T,int) 和 (T,double),只有一个模板定义。
我能做的是为 (T,int) 定义一个特化。见下文。但是,它应该适用于 (T,int) and (T,double) 只有一个函数定义(没有代码加倍)。
template <typename T,typename T2>
struct Foo
{
static inline void apply(T a, T2 b)
{
cout << "we are in the generic template definition" << endl;
}
};
// partial (T,*)
template <typename T>
struct Foo<T, int > // here something needed like T2=(int, double)
{
static inline void apply(T a, T2 b)
{
cout << "we are in the partial specialisation for (T,int)" << endl;
}
};
任何想法如何使用一个模板定义来部分专门化 (T,int) 和 (T,double) ?
【问题讨论】:
-
编译器怎么可能知道在这里做什么?它怎么知道你想打印
"partial double"等? -
输出仅用于了解使用了哪个函数定义。