从泛型命名空间向前声明类(在std::hash中)

Forward-declaring class from generic namespace (in std::hash

本文关键字:std hash 命名空间 泛型 声明      更新时间:2023-10-16

我正在努力向前声明。B引用A, A使用B的std::vector A和B都定义在泛型(无)命名空间中

在A的头文件中前向声明B对A中的成员是有效的。然而,我在同一个头文件中为A定义了哈希函数,这就造成了麻烦。

#include "B.h"
class B;
class A{
public:
   std::vector<B> bs; 
}
namespace std
{
template <>
struct hash<A>
{
    size_t operator()(const A& k) const
    {
        std::size_t seed = 0;
        boost::hash_combine(seed, k.foo);
        boost::hash_combine(seed, k.bar);
        for(B &b:k.bs){
            boost::hash_combine(seed, b.abc);
        }
        return seed;
    }
};
}

函数访问B的向量,因此也需要前向声明。然而,它不使用父头文件中的forward声明。不幸的是,我不能在名称空间std中再次前声明它,因为这会在定义之间造成歧义。任何想法?

您可以将hash<A>::operator()的定义移动到源文件中。所以:

// A.h
#include <vector>
#include <functional>
struct B;
struct A {
    std::vector<B> bs;
};
namespace std {
    template <>
    struct hash<A> {
        size_t operator()(const A& ) const;
    };
}
// A.cpp
#include "B.h"
// now, B is complete, so we can define operator()
size_t std::hash<A>::operator()(const A& k) const
{
    std::size_t seed = 0;
    boost::hash_combine(seed, k.foo);
    boost::hash_combine(seed, k.bar);
    for(const B& b : k.bs){
        boost::hash_combine(seed, b.abc);
    }
    return seed;
}