基于继承类的模板特化

时间:2008-11-11 18:17:47

标签: c++ templates specialization

我想让这个专门的w / o改变主要。是否可以基于其基类来专门化某些东西?我希望如此。

-edit -

我将有几个继承自SomeTag的类。我不想为每个人写相同的专业。

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;
}

4 个答案:

答案 0 :(得分:23)

本文介绍了一个巧妙的技巧:http://www.gotw.ca/publications/mxc++-item-4.htm

这是基本想法。首先需要一个IsDerivedFrom类(这提供了运行时和编译时检查):

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需要一个可能专门的实现:

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实际上看起来像:

template<typename T>
class MyClass: public MyClassImpl<T, IsDerivedFrom<T, SomeTag>::Is>
{
};

然后你的主人将会很好:

int main()
{
    MyClass<SomeTag>::isSpecialized test1; //ok
    MyClass<InheritSomeTag>::isSpecialized test2; //ok also
    return 0;
}

答案 1 :(得分:20)

嗯,上面的答案中的文章出现在2002年2月。虽然它有效但今天我们知道有更好的方法。或者,您可以使用enable_if

template<bool C, typename T = void>
struct enable_if {
  typedef T type;
};

template<typename T>
struct enable_if<false, T> { };

template<typename, typename>
struct is_same {
    static bool const value = false;
};

template<typename A>
struct is_same<A, A> {
    static bool const value = true;
};

template<typename B, typename D>                                 
struct is_base_of {                                                       
    static D * create_d();                     
    static char (& chk(B *))[1]; 
    static char (& chk(...))[2];           
    static bool const value = sizeof chk(create_d()) == 1 &&  
                              !is_same<B    volatile const, 
                                       void volatile const>::value;
};

struct SomeTag { };
struct InheritSomeTag : SomeTag { };

template<typename T, typename = void>
struct MyClass { /* T not derived from SomeTag */ };

template<typename T>
struct MyClass<T, typename enable_if<is_base_of<SomeTag, T>::value>::type> {
    typedef int isSpecialized;
};

int main() {
    MyClass<SomeTag>::isSpecialized test1;        /* ok */
    MyClass<InheritSomeTag>::isSpecialized test2; /* ok */
}

答案 2 :(得分:18)

现在是2014年的短版本,使用C ++ - 11:

#include <type_traits>

struct SomeTag { };
struct InheritSomeTag : SomeTag { };

template<typename T, bool = std::is_base_of<SomeTag, T>::value>
struct MyClass { };

template<typename T>
struct MyClass<T, true> {
    typedef int isSpecialized;
};

int main() {
    MyClass<SomeTag>::isSpecialized test1;        /* ok */
    MyClass<InheritSomeTag>::isSpecialized test2; /* ok */
}

答案 3 :(得分:4)

在您的情况下,我看到的唯一方法是为MyClass明确专门化InheritSomeTag。但是,SeqAn paper提出了一种称为“模板子类化”的机制,它可以执行您想要的操作 - 尽管使用了不同的继承语法,因此代码与您当前的main函数不兼容。

// Base class
template <typename TSpec = void>
class SomeTag { };

// Type tag, NOT part of the inheritance chain
template <typename TSpec = void>
struct InheritSomeTag { };

// Derived class, uses type tag
template <typename TSpec>
class SomeTag<InheritSomeTag<TSpec> > : public SomeTag<void> { };

template <class T, class Tag=T>
struct MyClass { };

template <class T, typename TSpec>
struct MyClass<T, SomeTag<TSpec> >
{
    typedef int isSpecialized;
};

int main()
{
    MyClass<SomeTag<> >::isSpecialized test1; //ok
    MyClass<SomeTag<InheritSomeTag<> > >::isSpecialized test2; //ok
}

这当然看起来很奇怪并且非常麻烦,但它允许使用在编译时执行的多态函数的真正继承机制。如果您想看到这一点,请查看一些SeqAn examples

话虽如此,我相信SeqAn是一个特例,没有多少应用程序可以从这种极其困难的语法中获益(解密SeqAn相关的编译器错误是* ss真正的痛苦!)

相关问题