Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何为所有派生类型部分专门化类模板?_C++_Derived Class_Partial Specialization_Crtp - Fatal编程技术网

C++ 如何为所有派生类型部分专门化类模板?

C++ 如何为所有派生类型部分专门化类模板?,c++,derived-class,partial-specialization,crtp,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

我想对一个基类和所有派生类部分地专门化一个我无法更改的现有模板(
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(); 
    }
};

} }

…但这不起作用,因为如果::type是
派生的
,编译器无法判断
enable\u。如果我可以更改
std::tr1::hash
,我只需要添加另一个伪模板参数来使用
boost::enable_If
,正如
enable_If
文档所建议的那样,但这显然不是一个很好的解决方案。有办法解决这个问题吗?我是否必须在我创建的每个
无序集
无序映射
上指定自定义哈希模板,或者为每个派生类完全专门化
hash

而不是修改
std::tr1::hash
,您应该创建自己的命名空间,并在其中定义新的结构
hash
,该结构继承自
std::tr1::hash
,或者专门用于
CRTPBase


模板
结构CRTPBase
{
大小\u t哈希(){return 0;}
};
结构AA:CRTPBase{};
结构BB{};
//
名称空间mynamespace{
模板
结构哈希:std::tr1::hash{};
//
模板
结构哈希::类型>
{    
大小运算符()(常量CRTPBase和base)常量
{         
返回base.hash();
}
};
}//名称空间mynamespace{}
//
//
void ff()
{
使用名称空间mynamespace;
hash aa;//我的hash
hash bb;//std::tr1::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

}

模板
结构CRTPBase
{
大小\u t哈希()常量{return 0;}
};
//第一例
//
//帮助班
结构DummyF1{};
结构DummyF2{};
结构DummyF3{};
模板结构X;
//主要类别
模板结构X:CRTPBase{
int a1;
};
模板结构X:CRTPBase{
int b1;
};
//typedefs
typedef×F1;
类型def X F2;
typedef DummyF3 F3;//不起作用
命名空间std{namespace tr1{
模板
结构哈希{
size_t运算符()(常量CRTPBase&base)常量
{         
返回base.hash();
}
};
}}//名称空间tr1//名称空间std
//
//第二种情况
结构DummyS1:CRTPBase{
int m1;
};
//
模板
结构Y:T{};
//
typedef Y S1;
命名空间std{namespace tr1{
模板
结构哈希{
大小运算符()(常量CRTPBase和base)常量
{         
返回base.hash();
}
};
}}//名称空间tr1//名称空间std
void main 1()
{
使用std::tr1::hash;
F1;
F2 F2;
F3 F3;
散列hf1;大小v1=hf1(f1);//自定义散列函子
hash hf2;size_t v2=hf2(f2);//自定义哈希函子
散列hf3;大小v3=hf3(f3);//错误:标准散列函子
S1;
散列hs1;大小w1=hs1(s1);//自定义散列函子
}

但他仍然必须在每个无序的\u集上指定一个自定义哈希模板,因为它不会使用他的命名空间。

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

}

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

}