如何为所有派生类型部分专门化类模板? [英] How to partially specialize a class template for all derived types?

查看:106
本文介绍了如何为所有派生类型部分专门化类模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想部分专门化一个现有的模板,我不能更改( std :: tr1 :: hash )一个基类和所有派生类。原因是我使用奇怪的循环模板模式的多态性,哈希函数在CRTP基类中实现。如果我只想部分专门为一个CRTP基类,那么很容易,我可以写:

I want to partially specialize an existing template that I cannot change (std::tr1::hash) for a base class and all derived classes. The reason is that I'm using the curiously-recurring template pattern for polymorphism, and the hash function is implemented in the CRTP base class. If I only want to partially specialize for a the CRTP base class, then it's easy, I can just write:


namespace std { namespace tr1 {

template <typename Derived>
struct hash<CRTPBase<Derived> >
{
    size_t operator()(const CRTPBase<Derived> & base) const 
    { 
        return base.hash(); 
    }
};

} }

但是这个特化与实际派生类不匹配,只有 CRTPBase< Derived> 。我想要的是一种为 Derived 编写部分专门化的方法,当且仅当它源于 CRTPBase< Derived> 。我的伪代码是

But this specialization doesn't match actual derived classes, only CRTPBase<Derived>. What I want is a way of writing a partial specialization for Derived if and only if it derives from CRTPBase<Derived>. My pseudo-code is


namespace std { namespace tr1 {

template <typename Derived>
struct hash<typename boost::enable_if<std::tr1::is_base_of<CRTPBase<Derived>, Derived>,
    Derived>::type>
{
    size_t operator()(const CRTPBase<Derived> & base) const 
    { 
        return base.hash(); 
    }
};

} }

...但这不工作,因为编译器不能告诉 enable_if< condition,Derived> :: type Derived 。如果我可以改变 std :: tr1 :: hash ,我只是添加另一个虚拟模板参数使用 boost :: enable_if ,如 enable_if 文档所建议,但这显然不是一个很好的解决方案。有办法解决这个问题吗?是否必须在每个 unordered_set unordered_map 上指定自定义哈希模板我创建或完全专门化 hash 对于每个派生类?

...but that doesn't work because the compiler can't tell that enable_if<condition, Derived>::type is Derived. If I could change std::tr1::hash, I'd just add another dummy template parameter to use boost::enable_if, as recommended by the enable_if documentation, but that's obviously not a very good solution. Is there a way around this problem? Do I have to specify a custom hash template on every unordered_set or unordered_map I create, or fully specialize hash for every derived class?

推荐答案

您可以选择更适合您。


template <typename Derived>
struct CRTPBase
{
    size_t hash() const {return 0; }
};

// First case 
//
// Help classes
struct DummyF1 {};
struct DummyF2 {};
struct DummyF3 {};
template<typename T> struct X; 

// Main classes
template<> struct X<DummyF1> : CRTPBase< X<DummyF1> > {
    int a1;
};

template<> struct X<DummyF2> : CRTPBase< X<DummyF2> > {
    int b1;
};

// typedefs
typedef X<DummyF1> F1;
typedef X<DummyF2> F2;
typedef DummyF3    F3; // Does not work

namespace std { namespace tr1 {
    template<class T>
    struct hash< X<T> > {
    	size_t operator()(const CRTPBase< X<T> > & base) const     
    	{         
    		return base.hash();     
    	}
    };
}} // namespace tr1 // namespace std 

//

// Second case
struct DummyS1 : CRTPBase <DummyS1> {
    int m1;
};
//
template<typename T> 
struct Y : T {};
//
typedef Y<DummyS1> S1;


namespace std { namespace tr1 {
    template<class T>
    struct hash< Y<T> > {
    	size_t operator()(const CRTPBase<T> & base) const     
    	{         
    		return base.hash();     
    	}
    };
}} // namespace tr1 // namespace std 

void main1()
{
    using std::tr1::hash;
    F1 f1;
    F2 f2;
    F3 f3;
    hash<F1> hf1; size_t v1 = hf1(f1); // custom hash functor
    hash<F2> hf2; size_t v2 = hf2(f2); // custom hash functor
    hash<F3> hf3; size_t v3 = hf3(f3); // error: standard hash functor

    S1 s1;
    hash<S1> hs1; size_t w1 = hs1(s1); // custom hash functor

}

这篇关于如何为所有派生类型部分专门化类模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆