标准在定义可见性方面对unordered_set的Value类型有什么要求

What requirements does the standard impose on the Value type of unordered_set in terms of definition visibility

我正在创建一个 class,其中包含一个 unordered_set,它有自己的散列和谓词,如下所示:

//SetHolder.h
#include <unordered_set>

struct SetHolder
{
    SetHolder(); //Defined in SetHolder.cpp
    ~SetHolder(); //Defined in SetHolder.cpp

    struct ArtifactImpl; //Defined in SetHolder.cpp
    struct ArtifactSetKeyOps
    {
      std::size_t operator()(
        const ArtifactImpl& artifact) const noexcept;
      bool operator()(
        const ArtifactImpl& lhs, const ArtifactImpl& rhs) const;
    };

    std::unordered_set<ArtifactImpl,
      ArtifactSetKeyOps,ArtifactSetKeyOps> artifactSet_;
};

我在 ubuntu linux (stdlibc++) 下使用 gcc 4.8.2 编译器,而且我观察到,如果我只使用 cpp 文件中的 unordered_set,它只会在声明 ArtifactImpl 时编译。然而,我需要提供 ArtifactSetKeyOps 的定义(为什么,我不明白)。

std::unordered_mapstd::unordered_set 在这方面对其模板参数没有特殊要求,因此适用一般库范围的规则。这是 C++14 17.6.4.8/2.5:

In particular, the effects are undefined in the following cases:

...

  • if an incomplete type (3.9) is used as a template argument when instantiating a template component, unless specifically allowed for that component.

因此,如果值类型、散列类型或谓词类型不完整(在您的情况下是这样),您就有未定义的行为。

因为这个要点特别指出 "template component" 而该子句的其余部分指的是 函数, 我会说这意味着 class 模板也是一个 "template component." 在你的例子中,class 模板是在 SetHolder [=27= 的定义中的 artifactSet_ 成员的定义中实例化的].