简体   繁体   中英

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> . Searches of GCC's Bugzilla turn up nothing that seem to address this.

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> . None of the other includes caused this problem when placed INSIDE the namespace, only <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.

Might this be an libstdc++ bug? GCC bug?

You should not place standard #include directives inside a namespace. See C++14 [using.headers]/3 (which is speaking about the standard library's headers):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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