std::unordered_set;作为 Foo 类的成员
我正在编写一个类,该类具有自己类型的 unordered_set 作为成员。 因此我需要为 hash
编写一个专门化的版本。这个特化需要在声明 Foo 之后定义。但在我看来,在定义成员 unordered_set
之前,我似乎已经需要对 hash
进行专门化。至少它不会编译并在那里失败。我尝试了哈希模板的前向声明,但也无法使其工作。
相关代码片段是:
class Foo {
public:
int i;
std::unordered_set<Foo> dummy;
Peer(std::unordered_set<Foo>);
};
namespace std {
template<> struct hash<Foo>
{
size_t operator()(const Foo& f) const
{
return hash<int>()(f.i);
}
};
}
提前致谢
I'm writing a class that has an unordered_set of its own type as a member.
Therefore I need to write a specialization for hash<Foo>
. This specialization needs to be defined after Foo is declared. But it seems to me as if I already need the specialization for hash<Foo>
before defining the member unordered_set<Foo>
. At least it doesn't compile and fails there. I tried a forward declaration of the hash template but couldn't get it working thereby either.
The relevant code snippet is:
class Foo {
public:
int i;
std::unordered_set<Foo> dummy;
Peer(std::unordered_set<Foo>);
};
namespace std {
template<> struct hash<Foo>
{
size_t operator()(const Foo& f) const
{
return hash<int>()(f.i);
}
};
}
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Foo
不能有std::unordered_set
类型的成员变量。您无法实例化具有不完整类型的标准库容器。基本上,除了几个与此处不相关的例外之外,类类型在终止其定义的
}
之前并不完整。您要么需要在容器中存储一些其他类型(可能是 std::unique_ptr),要么使用提供可使用不完整类型实例化的容器的容器库(例如,Boost 有这样的)容器库)。
Foo
cannot have a member variable of typestd::unordered_set<Foo>
.You cannot instantiate a Standard Library container with an incomplete type. Basically, with several exceptions not relevant here, a class type is not complete until the
}
that terminates its definition.You'll either need to store some other type in the container (perhaps
std::unique_ptr<Foo>
), or use a containers library that provides containers instantiable with an incomplete type (e.g., Boost has such a containers library).您可以稍微移动声明以使其编译:
不过,正如 James 所说,
dummy
的声明是未定义的行为。您还需要进行相等比较;最简单的方法是向
Foo
添加一个operator==
。我还建议让 Foo 的构造函数通过 const 引用获取参数。
You can move the declaration around a bit to make it compile:
As James says, though, the declaration of
dummy
is is undefined behaviour.You will also need an equality comparison; easiest to add an
operator==
toFoo
.I would also recommend making the constructor of
Foo
take the argument by const-reference.