整个参数集的模板专门化
template specialisation for a whole set of parameters
可能很容易解决,但很难找到解决方案:
是否有可能(部分地)专门化一整组类型?在这个例子中,"Foo"应该部分特化为(T,int)和(T,double),只有一个模板定义。
我能做的是为(T,int)定义一个专门化。见下文。但是,对于(T,int) 和 (T,double),它应该只有一个函数定义(没有代码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)有什么想法吗?
如果我正确理解了你的问题,那么你可以编写一个基类模板并从中派生,如下所示:
template <typename T, typename U>
struct Foo_Base
{
static inline void apply(T a)
{
cout << "we are in the partial specialisation Foo_Base(T)" << endl;
}
};
template <typename T>
struct Foo<T, int> : Foo_Base<T, int> {};
template <typename T>
struct Foo<T, double> : Foo_Base<T, double> {};
虽然它不是一个模板定义(如你所要求的),但你可以避免代码重复。
Demo: http://www.ideone.com/s4anA
我相信您可以使用Boost的enable_if来为您想要的类型启用部分专门化。第3.1节展示了如何做,并给出了这个例子:
template <class T, class Enable = void>
class A { ... };
template <class T>
class A<T, typename enable_if<is_integral<T> >::type> { ... };
相关文章:
- 是否可以对零模板参数进行模板专门化
- 我能否根据其运算符()的签名专门化可变参数模板参数
- 取消专门化C++模板参数
- 模板函数,其中一个参数需要专门化,而另一个不需要
- 如何为特定数量的模板参数专门化可变参数模板结构
- 使用非类型模板参数进行专门化模板模板参数
- g++和clang++在结构/类专门化中具有非类型参数的不同行为
- 我如何避免使用依赖于参数的查找明确专门化模板化功能
- 专门化采用通用引用参数的函数模板
- 如果需要参数包,则使用SFINAE专门化类
- c++多参数模板化的类成员专门化
- C++11`using`关键字:专门化模板参数的模板别名
- 如何在没有参数的情况下专门化模板类
- 我可以用非模板类专门化一个可变模板模板模板参数吗
- 如何在C++中专门化 std::vector 的模板模板参数
- 按参数C++类型专门化重载构造函数
- 将“类型参数”与“非类型参数”(或相反)专门化
- 如何在非模板化类中专门化没有参数的模板化方法
- 在 C++ 中鸭子键入(通过其非类型模板参数的值专门化模板函数)
- 使用已推导的模板参数专门化模板成员函数