如何为所有派生类型部分特化类模板?

发布于 2024-07-25 01:40:59 字数 1321 浏览 6 评论 0原文

我想部分专门化一个无法为基类和所有派生类更改的现有模板 (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 时。 我的伪代码是


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::typeDerived。 如果我可以更改 std::tr1::hash,我只需添加另一个虚拟模板参数即可使用 boost::enable_if,正如 enable_if 所建议的那样 文档,但这显然不是一个很好的解决方案。 有办法解决这个问题吗? 我是否必须在创建的每个 unordered_setunordered_map 上指定自定义哈希模板,或者为每个派生类完全专门化 hash

I want to partially specialize an existing template that I cannot change (std::tr1::hash) for a base class and all derived classes. The reason is that I'm using the curiously-recurring template pattern for polymorphism, and the hash function is implemented in the CRTP base class. If I only want to partially specialize for a the CRTP base class, then it's easy, I can just write:


namespace std { namespace tr1 {

template <typename Derived>
struct hash<CRTPBase<Derived> >
{
    size_t operator()(const CRTPBase<Derived> & base) const 
    { 
        return base.hash(); 
    }
};

} }

But this specialization doesn't match actual derived classes, only CRTPBase<Derived>. What I want is a way of writing a partial specialization for Derived if and only if it derives from CRTPBase<Derived>. My pseudo-code is


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(); 
    }
};

} }

...but that doesn't work because the compiler can't tell that enable_if<condition, Derived>::type is Derived. If I could change std::tr1::hash, I'd just add another dummy template parameter to use boost::enable_if, as recommended by the enable_if documentation, but that's obviously not a very good solution. Is there a way around this problem? Do I have to specify a custom hash template on every unordered_set or unordered_map I create, or fully specialize hash for every derived class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小帐篷 2024-08-01 01:40:59

以下代码有两种变体。 您可以选择更适合您的。


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

}

There are two variants in the following code. You could choose more appropriated for you.


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

}
╰ゝ天使的微笑 2024-08-01 01:40:59

您应该创建自己的命名空间并定义从 std::tr1::hash< 继承的新结构 hash ,而不是修改 std::tr1::hash /code> 或专用于 CRTPBase


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

}

Instead of modifying std::tr1::hash you should make your own namespace and define there new structure hash which inherited from std::tr1::hash or is specialized for 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

}
偏闹i 2024-08-01 01:40:59

C++20版本:

#include <concepts>
#include <functional>
#include <unordered_set> // just for demo in main()

template <class T>
class Base {};

class Derived final : public Base<Derived> {};

template<class T>
requires std::derived_from<T, Base<T>>
struct std::hash<T> {
  // constexpr is optional
  constexpr size_t operator() (const T& value) const noexcept {
    return 0xDEADBEEF; // FIXME: do something better :)
  }
};

int main() {
  // If operator() weren't constexpr, this couldn't be a *static* assert
  static_assert(std::hash<Derived>()(Derived {}) == 0xDEADBEEF);
  std::unordered_set<Derived> usageTest;
  return 0;
}

C++20 edition:

#include <concepts>
#include <functional>
#include <unordered_set> // just for demo in main()

template <class T>
class Base {};

class Derived final : public Base<Derived> {};

template<class T>
requires std::derived_from<T, Base<T>>
struct std::hash<T> {
  // constexpr is optional
  constexpr size_t operator() (const T& value) const noexcept {
    return 0xDEADBEEF; // FIXME: do something better :)
  }
};

int main() {
  // If operator() weren't constexpr, this couldn't be a *static* assert
  static_assert(std::hash<Derived>()(Derived {}) == 0xDEADBEEF);
  std::unordered_set<Derived> usageTest;
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文