c++ - 如何为所有派生类型部分特化类模板?

标签 c++ derived-class partial-specialization crtp

我想为基类和所有派生类部分特化我无法更改的现有模板 ( std::tr1::hash )。原因是我正在为多态性使用奇怪的重复模板模式,并且散列函数是在 CRTP 基类中实现的。如果我只想部分专门化 CRTP 基类,那么很简单,我可以这样写:


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> .我的伪代码是


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>::typeDerived .如果我能改变 std::tr1::hash , 我只是添加另一个虚拟模板参数来使用 boost::enable_if , 正如 enable_if 所推荐的那样文档,但这显然不是一个很好的解决方案。有办法解决这个问题吗?我是否必须在每个 unordered_set 上指定自定义哈希模板?或 unordered_map我创建或完全特化 hash对于每个派生类?

最佳答案

以下代码有两种变体。您可以选择更适合您的。


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

}

关于c++ - 如何为所有派生类型部分特化类模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1032973/

相关文章:

C++ 模板特化 - 将其他整数类型委托(delegate)给 uint64_t

c++ - 扩展 boost::dynamic_bitset 以添加一些功能

c++ - "ask which exact type an object has"是否总是表明设计不好?

C++:不允许 void 的部分函数特化 - 替代解决方案?

c++ - 令人困惑的 std::map 插入行为

c++ - 是否允许 C++ 优化器跨函数调用移动语句?

C++:TCP文件传输中的字节顺序差异

c++ - 复制基类的构造函数

c++ - 成员模板特化

部分特化时基于 C++ 模板的 "override"等效?