Bug 81347 - g++ confused by namespaces and friend classes
Summary: g++ confused by namespaces and friend classes
Status: RESOLVED DUPLICATE of bug 59930
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.4.0
: P3 normal
Target Milestone: 8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2017-07-06 22:43 UTC by Thomas Anderson
Modified: 2018-03-20 20:50 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-07-17 00:00:00


Attachments
Preprocessed test.cpp (79.11 KB, text/plain)
2017-07-06 22:43 UTC, Thomas Anderson
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Anderson 2017-07-06 22:43:47 UTC
Created attachment 41695 [details]
Preprocessed test.cpp

Compile test.cpp below using libc++ headers:

-----test.cpp-----
#include <set>

class C {
  std::set<int> s;
};

#include <map>

using std::map;
------------------

$ g++ -c test.cpp -nostdinc++ -isystem/path/to/libc++/trunk/include

g++ fails to compile and gives:
main.cpp:9:12: error: ‘map’ is already declared in this scope
 using std::map;

I've attached a preprocessed output for convenience.

$ g++ -c test_preproc.cpp 2>/dev/null && echo pass || echo fail
fail
$ clang++ -c test_preproc.cpp 2>/dev/null && echo pass || echo fail
pass
Comment 1 Richard Biener 2017-07-17 09:10:05 UTC
Sounds like a bug in libc++ headers to me -> file to the LLVM project.
Comment 2 Jonathan Wakely 2017-07-17 12:09:06 UTC
No, this is definitely a GCC bug:

namespace A {

  template <class>
    class tree
    {
      template <class> friend class  map;
    };

}

A::tree<int> x;

namespace A {
  template <class> class  map { };
}

using A::map;
Comment 3 Jonathan Wakely 2017-07-17 12:11:12 UTC
I would guess that the instantiation of A::tree injects the type map into the enclosing namespace, and then the using-declaration finds two declarations and thinks it's an error.

bug.cc:17:10: error: ‘map’ is already declared in this scope
 using A::map;
          ^~~
Comment 4 Richard Smith 2017-11-21 23:20:25 UTC
Duplicate of bug#59930 (friend class templates in class templates get injected into the global namespace instead of the innermost enclosing namespace)?
Comment 5 Jonathan Wakely 2018-03-20 20:50:39 UTC
Fixed by r255780 so closing as a dup, as Richard suggested.

*** This bug has been marked as a duplicate of bug 59930 ***