简体   繁体   English

错误<unordered_set> ?

[英]Bug in <unordered_set>?

I am building a header-only library (for good reasons; don't hate) which contains a class and the implementations of the class member functions.我正在构建一个只有头文件的库(有充分的理由;不要讨厌),其中包含一个类和类成员函数的实现。 In doing so I ran into a very odd error with <unordered_set> .在这样做时,我遇到了<unordered_set>一个非常奇怪的错误。 Searches of GCC's Bugzilla turn up nothing that seem to address this.对 GCC 的 Bugzilla 的搜索似乎没有解决这个问题。

My code which breaks (badly) has the includes inside my namespace.我的代码中断(严重)在我的命名空间中有包含。

namespace probability {

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

class ProbabilityTools
{
...

By chance, I moved the #includes outside of the class namespace and it fixed the problem with <unordered_set> .偶然地,我将 #includes 移到了类命名空间之外,它解决了<unordered_set>的问题。 None of the other includes caused this problem when placed INSIDE the namespace, only <unordered_set> .当放置在名称空间内时,其他任何包含都不会导致此问题,只有<unordered_set>

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

namespace probability {

class ProbabilityTools
{
...

I am using GCC g++ 4.8 with -std=c++11 to build this code which works in the second configuration and works as far as <unordered_map> use, in both configurations.我正在使用带有 -std=c++11 的 GCC g++ 4.8 来构建此代码,该代码在第二个配置中工作,并且在两种配置中都可以使用<unordered_map>

Might this be an libstdc++ bug?这可能是一个 libstdc++ 错误吗? GCC bug?海湾合作委员会错误?

You should not place standard #include directives inside a namespace.您不应将标准#include指令放在命名空间内。 See C++14 [using.headers]/3 (which is speaking about the standard library's headers):参见 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.翻译单元应仅在任何外部声明或定义之外包含标题,并且应在词法上在首次引用它在该翻译单元中声明或首先定义的任何实体之前包含标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM