Problem with combination of namespaces, templates, and friends
Martin Staley
staley@t7.lanl.gov
Wed Mar 29 19:22:00 GMT 2000
Hello -
I submitted the following code to your online test compilation form
at http://www.codesourcery.com/gcc-compile.html , with C++ specified
as the language, and with no additional compiler flags:
namespace A {
template<class T>
struct thing;
}
namespace B {
template<class T>
void fun(const A::thing<T> &);
}
namespace A {
template<class T>
struct thing {
friend void B::fun<T>(const thing &);
};
}
namespace B {
template<class T>
void fun(const A::thing<T> &)
{
}
}
int main(void) {
A::thing<int> t;
B::fun(t);
}
Here are the output compiler messages:
/tmp/@31205.1: In instantiation of `A::thing':
/tmp/@31205.1:26: instantiated from here
/tmp/@31205.1:14: `fun' was not declared in this scope
With the (slightly older) version of g++ on my machine (g++ -v gives
"gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)"), the
command "g++ bug.cc", where bug.cc contains the above code, gave the
following compiler messages:
bug.cc:14: declaration of `fun(const ::A::thing<T> &)' not in a
namespace surrounding `::B'
bug.cc: In instantiation of `::A::::A::thing<int>':
bug.cc:26: instantiated from here
bug.cc:14: `fun' was not declared in this scope
Am I wrong in believing the above is legal C++? Incidentally it
compiles with KAI C++ (EDG front-end, which you're probably familiar
with), with all "strict ansi" flags turned on. That doesn't prove
it's legal, of course, but surely the language doesn't require that
a function be in the same namespace as any class with which it's a
friend...?
I browsed around on http://gcc.gnu.org/ and on the gcc/g++ info pages
but couldn't find any mention of this bug (feature?).
Martin
------------------------------------------------------------------------
Martin Staley (tel) 505-665-4963
T-7, Mathematical Modeling and Analysis (fax) 505-665-5757
M.S. B-284 staley@t7.lanl.gov
Los Alamos National Laboratory
Los Alamos, NM 87544 http://cnls-www.lanl.gov/~staley
------------------------------------------------------------------------
More information about the Gcc-bugs
mailing list