Bug 807 - g++ 2.96 (20000731) + 2.97 (20001112) name resolution bug in friend definition in specialized template class.
Summary: g++ 2.96 (20000731) + 2.97 (20001112) name resolution bug in friend definitio...
Status: RESOLVED DUPLICATE of bug 764
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 2.97
: P3 normal
Target Milestone: ---
Assignee: Nathan Sidwell
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2000-11-14 14:56 UTC by Bryan Green
Modified: 2003-06-12 00:15 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bryan Green 2000-11-14 14:56:00 UTC
The compiler is reporting "no match" for an friend operator==() which happens to be correctly defined.
The class for which the operator is defined is a specialization of a class template.
This inability to find the right operator only occurs, to my knowledge, when the operator is defined
as a friend function inside a template class specialization.
The error also goes away with one very simple yet apparently meaningless change, which in my example
code can be enabled with '-DTEST'.

[bgreen@pc125 tmp]$ g++ -c mytest2.C
mytest2.C: In function `bool operator== (const cell &, const cell &)':
mytest2.C:35: no match for `const myval<int> & == const myval<int> &'
mytest2.C:34: candidates are: bool operator== (const cell &, const cell &)
[bgreen@pc125 tmp]$
[bgreen@pc125 tmp]$ g++ -DTEST -c mytest2.C
[bgreen@pc125 tmp]$

Release:
gcc version 2.97 20001112 (experimental)

Environment:
uname -a:
Linux pc125.nas.nasa.gov 2.2.16-22smp #1 SMP Tue Aug 22 16:39:21 EDT 2000 i686 unknown

How-To-Repeat:
template <class T>
class myval
{
  T d;

public:
  myval(const myval &v) : d(v.d) {}
  friend bool operator==(const myval<T>& lhs, const myval<T>& rhs) {
    return lhs.d == rhs.d;
  }
};

template<>
class myval<int>
{
  int d;

public:
  myval(const myval &v) : d(v.d) {}
  friend bool operator==(const myval<int>& lhs, const myval<int>& rhs) {
    return lhs.d == rhs.d;
  }
};


class cell {
  myval<int> v;

public:
#ifdef TEST
  cell(const myval<int> &in) : v(in) {}
#endif
  friend bool operator==(const cell& lhs, const cell& rhs) {
    return lhs.v == rhs.v;
  }
};
Comment 1 Bryan Green 2000-11-14 14:56:00 UTC
Fix:
Defining the friend function outside the class does not work either,
but rather highlights a different bug.
It is possible to define operator==() as a member function instead,
and that works as a workaround.
Comment 2 Nathan Sidwell 2000-11-24 01:51:23 UTC
*** This bug has been marked as a duplicate of 764 ***
Comment 3 Nathan Sidwell 2000-11-24 09:51:23 UTC
From: nathan@gcc.gnu.org
To: bgreen@nas.nasa.gov, gcc-gnats@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: c++/807
Date: 24 Nov 2000 09:51:23 -0000

 Synopsis: g++ 2.96 (20000731) + 2.97 (20001112) name resolution bug in friend definition in specialized template class.
 
 State-Changed-From-To: open->analyzed
 State-Changed-By: nathan
 State-Changed-When: Fri Nov 24 01:51:23 2000
 State-Changed-Why:
     confirmed as a bug -- possible duplicate of 764
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=807&database=gcc
Comment 4 Craig Rodrigues 2001-05-25 01:13:01 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: Works under:
    gcc version 3.0 20010507 (prerelease)
Comment 5 Craig Rodrigues 2001-05-25 08:13:01 UTC
From: rodrigc@gcc.gnu.org
To: bgreen@nas.nasa.gov, gcc-gnats@gcc.gnu.org, nobody@gcc.gnu.org,
  rodrigc@gcc.gnu.org
Cc:  
Subject: Re: c++/807
Date: 25 May 2001 08:13:01 -0000

 Synopsis: g++ 2.96 (20000731) + 2.97 (20001112) name resolution bug in friend definition in specialized template class.
 
 State-Changed-From-To: analyzed->closed
 State-Changed-By: rodrigc
 State-Changed-When: Fri May 25 01:13:01 2001
 State-Changed-Why:
     Works under:
     gcc version 3.0 20010507 (prerelease) 
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=807&database=gcc