This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Problem with namespaces, templates, and friend functions.
- To: bug-gcc at gnu dot org
- Subject: Problem with namespaces, templates, and friend functions.
- From: Anders Lindgren <andersl at iar dot se>
- Date: 20 Jan 2000 15:14:58 +0100
Host type: Sun.
Gcc version: 2.95.2
Command line parameters: None (expcept the name of the program).
//
// In the follwoing program GCC seems to forget the function
// "Forgotten". In the function "foo" it can be called with objects
// of type U1 and U2. But in "bar" it can only be called with "U2"
// objects.
//
// It seems as GCC remembers only the prototype of the last usage of
// the function "Forgotten" in the first function where it is used. (As
// a compiler developer, I'd put my money on a faulty cache mechanism
// in your function matching system.)
//
// Try the following:
// Remove the namespace declaration. =>
// The program compiles with no errors.
// Comment out function "foo". => Ditto.
// Change U1 to U2 in "bar". => Ditto.
// Swap order of the calls in in "foo". => Ditto.
// Replace the friend form of "Forgotten" inside "Object" (Alt 1)
// with the template function definition (Alt 2). => Ditto.
//
namespace MySpace
{
class Tag1 { };
class Tag2 { };
template<class Tag>
class Object
{
public:
// Alt 1: Broken:
friend void Forgotten(Object const & m)
{ /* Do something */ }
};
// Alt 2: This definition works:
// template<class Tag> void Forgotten(Object<Tag> const & m)
// { /* Do something */ }
typedef Object<Tag1> U1;
typedef Object<Tag2> U2;
void foo()
{
Forgotten(U1());
Forgotten(U2());
}
void bar()
{
Forgotten(U1());
}
}