2009-06-23 3 views
6

기본 클래스와 파생 된 모든 클래스에 대해 변경할 수없는 기존 템플릿을 부분적으로 특수화하려고합니다 (std::tr1::hash). 그 이유는 내가 다형성을 위해 흥미롭게 반복되는 템플릿 패턴을 사용하고 있고, 해시 함수가 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> 일치하지 않는 : 나는 부분적으로 만 상기 CRTP 기본 클래스에 대한 전문하려는 경우, 그것은 그냥 쓸 수 쉽습니다. 내가 원하는 것은 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을 완전히 전문화해야합니까? 대신 당신이 당신의 자신의 네임 스페이스를하고 std::tr1::hash에서 상속 또는 CRTPBase<Derived>에 대한 전문이 새로운 구조 hash을 정의해야합니다 std::tr1::hash을 수정

답변

7

다음 코드에는 두 가지 변형이 있습니다. 당신은 당신에게 더 충당 할 수 있습니다.


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

.


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에 사용자 지정 해시 템플릿을 지정해야합니다. –

관련 문제