2009-06-23 8 views
6

基本クラスとすべての派生クラスに対して変更できない既存のテンプレート(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>と一致していません。私が欲しいのは、CRTPBase<Derived>から派生した場合に限り、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を変更できる場合は、enable_ifのドキュメントで推奨されているように、別のダミーテンプレートパラメータをboost::enable_ifに追加するだけですが、それは非常に良い解決策ではありません。この問題を回避する方法はありますか?派生クラスごとにunordered_setまたはunordered_mapのカスタムハッシュテンプレートを指定するか、hashを完全に特化する必要がありますか?

答えて

7

次のコードには2つのバリエーションがあります。あなたはあなたのためにもっと充当することができます。


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 

} 
+0

いいね、ありがとう。 – Doug

1

std::tr1::hashを変更する代わりに、独自の名前空間を作成し、std::tr1::hashから継承されたに特化した新しい構造hashを定義する必要があります。


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

struct AA : CRTPBase <AA> {}; 
struct BB {}; 
// 
namespace mynamespace { 

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

} // namespace mynamespace {} 
// 
// 
void ff() 
{ 
    using namespace mynamespace; 

    hash<AA> aa; // my hash 
    hash<BB> bb; // std::tr1::hash 

} 
+1

しかし、彼はまだネームスペースを使用しないので、すべてのunordered_setにカスタムハッシュテンプレートを指定する必要があります。 –

関連する問題