基于继承类的模板专业化 [英] Template specialization based on inherit class
问题描述
我想做这个专门的无变化主线。是否有可能根据其基类来专门化某些东西?我希望是这样。
I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so.
-edit-
我将有几个继承自SomeTag的类。我不想为他们每个人写相同的专业化。
I'll have several classes that inherit from SomeTag. I don't want to write the same specialization for each of them.
class SomeTag {};
class InheritSomeTag : public SomeTag {};
template <class T, class Tag=T>
struct MyClass
{
};
template <class T>
struct MyClass<T, SomeTag>
{
typedef int isSpecialized;
};
int main()
{
MyClass<SomeTag>::isSpecialized test1; //ok
MyClass<InheritSomeTag>::isSpecialized test2; //how do i make this specialized w/o changing main()
return 0;
}
推荐答案
本文介绍了一种巧妙的技巧: http://www.gotw.ca/publications/mxc++-item-4。 htm
This article describes a neat trick: http://www.gotw.ca/publications/mxc++-item-4.htm
这是基本概念。您首先需要一个IsDerivedFrom类(该类提供运行时和编译时检查):
Here's the basic idea. You first need an IsDerivedFrom class (this provides runtime and compile-time checking):
template<typename D, typename B>
class IsDerivedFrom
{
class No { };
class Yes { No no[3]; };
static Yes Test( B* ); // not defined
static No Test( ... ); // not defined
static void Constraints(D* p) { B* pb = p; pb = p; }
public:
enum { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) };
IsDerivedFrom() { void(*p)(D*) = Constraints; }
};
然后,您的MyClass需要一个可能专门化的实现:
Then your MyClass needs an implementation that's potentially specialized:
template<typename T, int>
class MyClassImpl
{
// general case: T is not derived from SomeTag
};
template<typename T>
class MyClassImpl<T, 1>
{
// T is derived from SomeTag
public:
typedef int isSpecialized;
};
MyClass实际上看起来像:
and MyClass actually looks like:
template<typename T>
class MyClass: public MyClassImpl<T, IsDerivedFrom<T, SomeTag>::Is>
{
};
然后您的主管道将保持良好状态:
Then your main will be fine the way it is:
int main()
{
MyClass<SomeTag>::isSpecialized test1; //ok
MyClass<InheritSomeTag>::isSpecialized test2; //ok also
return 0;
}
这篇关于基于继承类的模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!