모든 파생 유형에 대한 클래스 템플릿을 부분적으로 전문화하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1032973

문제

변경할 수없는 기존 템플릿을 부분적으로 전문화하고 싶습니다 (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>::type ~이다 Derived. 내가 바꿀 수 있다면 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

}

다른 팁

수정하는 대신 std::tr1::hash 자신만의 네임 스페이스를 만들고 새로운 구조를 정의해야합니다. hash 상속 된 std::tr1::hash 또는 전문화되어 있습니다 CRTPBase<Derived>.


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

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top