<unordered_set> 中的错误?

Bug in <unordered_set>?

我正在构建一个 header-only 库(有充分的理由;不要讨厌),其中包含 class 和 class 成员函数的实现。在这样做的过程中,我 运行 与 <unordered_set> 发生了一个非常奇怪的错误。搜索 GCC 的 Bugzilla 似乎没有找到任何解决这个问题的方法。

我的代码(严重)包含在我的命名空间中。

namespace probability {

#include <string>
#include <unordered_set>  // only this include breaks
#include <unordered_map>  

class ProbabilityTools
{
...

我偶然将#includes 移到了class 命名空间之外,它解决了<unordered_set> 的问题。 None 其他包含在命名空间内放置时导致此问题,只有 <unordered_set>.

#include <string>
#include <unordered_set>   // works when outside the namespace
#include <unordered_map>

namespace probability {

class ProbabilityTools
{
...

我正在使用带有 -std=c++11 的 GCC g++ 4.8 来构建此代码,它在第二种配置中工作,并且在 <unordered_map> 使用的情况下,在两种配置中工作。

这可能是一个 libstdc++ 错误? GCC 错误?

您不应将标准 #include 指令放在命名空间内。参见 C++14 [using.headers]/3(这是关于标准库的头文件):

A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference to any of the entities it declares or first defines in that translation unit.