将派生类的std :: hash专门化 [英] Specializing std::hash to derived classes

查看:167
本文介绍了将派生类的std :: hash专门化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象基类 Hashable ,可以散列的类派生自。现在我想扩展 std :: hash 到所有派生自 Hashable 的类。



下面的代码应该完全做到这一点。

  #include< functional> 
#include< type_traits>
#include< iostream>
$ b $ class Hashable {
public:
virtual〜Hashable(){}
virtual std :: size_t Hash()const = 0;
};

类派生:public Hashable {
public:
std :: size_t Hash()const {
return 0;
}
};

//对std :: hash进行专门化以在Hashable或从
派生的任何类上进行操作// Hashable。
namespace std {
template< class C>
struct hash {
typename std :: enable_if< std :: is_base_of< Hashable,C> :: value,std :: size_t> :: type
operator()(const C& object )const {
return object.Hash();
}
};
}

int main(int,char **){
std :: hash< Derived>散列器;
派生的d;
std :: cout<< (d)<<的std :: ENDL;

返回0;
}

上述代码与gcc 4.8.1完全一样,但是当我尝试使用gcc 4.7.2进行编译,我得到以下内容:

$ $ $ $ $ $ c ++ -std = c ++ 11 -o测试test_hash.cpp
test_hash.cpp:22:8:错误:重新定义'struct std :: hash< _Tp>'
在/usr/include/c++/4.7/functional:59包含的文件中:0,来自test_hash.cpp的
:1:
/usr/include/c++/4.7/bits/functional_hash.h:58:12:error:'struct std :: hash< _Tp>的前一个定义;'
/usr/include/c++/4.7/bits/functional_hash.h:在'struct std :: hash< Derived>'的实例化中:
test_hash.cpp:31:24:从这里需要
/usr/include/c++/4.7/bits/functional_hash.h:60:7:错误:静态断言失败:std :: hash不是专门用于这种类型的

任何人都可以想出一种方法来使任何类的 std :: hash 派生自 Hashable wi th gcc 4.7.2?

解决方案

好像没有正确的方法来做我想做的事情。我已决定使用以下宏为每个派生类编写单独的特化:

  //宏可方便地为一个从Hashable派生的类
#define DEFINE_STD_HASH_SPECIALIZATION(hashable)\
namespace std {\
template<> \
struct hash< hashable> {\
std :: size_t operator()(const hashable& object)const {\
return object.Hash(); \
} \
}; \
}

然后

  //派生
的std :: hash特化。DEFINE_STD_HASH_SPECIALIZATION(Derived);


I have an abstract base class Hashable that classes that can be hashed derive from. I would now like to extend std::hash to all classes that derive from Hashable.

The following code is supposed to do exactly that.

#include <functional>
#include <type_traits>
#include <iostream>

class Hashable {
public:
    virtual ~Hashable() {}
    virtual std::size_t Hash() const =0;
};

class Derived : public Hashable {
public:
    std::size_t Hash() const {
        return 0;
    }
};

// Specialization of std::hash to operate on Hashable or any class derived from
// Hashable.
namespace std {
template<class C>
struct hash {
  typename std::enable_if<std::is_base_of<Hashable, C>::value, std::size_t>::type
  operator()(const C& object) const {
    return object.Hash();
  }
};
}

int main(int, char**) {
    std::hash<Derived> hasher;
    Derived d;
    std::cout << hasher(d) << std::endl;

    return 0;
}

The above code works exactly as expected with gcc 4.8.1, but when I try to compile it with gcc 4.7.2, I get the following:

$ g++ -std=c++11 -o test test_hash.cpp
test_hash.cpp:22:8: error: redefinition of ‘struct std::hash<_Tp>’
In file included from /usr/include/c++/4.7/functional:59:0,
                 from test_hash.cpp:1:
/usr/include/c++/4.7/bits/functional_hash.h:58:12: error: previous definition of ‘struct std::hash<_Tp>’
/usr/include/c++/4.7/bits/functional_hash.h: In instantiation of ‘struct  std::hash<Derived>’:
test_hash.cpp:31:24:   required from here
/usr/include/c++/4.7/bits/functional_hash.h:60:7: error: static assertion failed:  std::hash is not specialized for this type

Can anybody think of a way to make this specialization of std::hash work for any class derived from Hashable with gcc 4.7.2?

解决方案

It seems like there is no proper way to do what I wanted to do. I have decided to just write separate specializations for each derived class, using the following macro:

// macro to conveniently define specialization for a class derived from Hashable
#define DEFINE_STD_HASH_SPECIALIZATION(hashable)                               \
namespace std {                                                                \
template<>                                                                     \
struct hash<hashable> {                                                        \
  std::size_t operator()(const hashable& object) const {                       \
    return object.Hash();                                                      \
  }                                                                            \
};                                                                             \
}

and then

// specialization of std::hash for Derived
DEFINE_STD_HASH_SPECIALIZATION(Derived);

这篇关于将派生类的std :: hash专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆