Bug 8099 - Friend classes and template specializations
Bug#: 8099 Product:  gcc Version: unknown
Host:  Target:  Build: 
Status: RESOLVED Severity: normal Priority: P3
Resolution: FIXED Assigned To: lerdsuwa@gcc.gnu.org Reported By: bangerth@ticam.utexas.edu
Component: c++ Target Milestone: ---
Summary: Friend classes and template specializations
Keywords:  
Opened: 2002-09-30 10:16
Description:   Last confirmed: Opened: 2002-09-30 10:16
[Note that there are already a number of related bug reports,
at least c++/8056 comes to my mind, but there may be more.]

Maybe this is just some point where the standard is unclear:
in 14.5.3.1, the following example is given of a friend
declaration:
  template <typename T> class X {
    friend class X<int>;
  };
One would presume that this syntax also holds if a class
Y would name a particular (fully specialized) instance of
X as a friend. That would be irrespective of whether this
instance is derived from the general template for X, or
from a partial or full explicit specialization of X. Then
the following example should compile (as it does with
Compaq's cxx and Intel's icc):
--------------------------------
template <int N, typename T> class X;
template <typename T>        class X<1,T>;

template <typename P> class Y {
    static int i;
    template <int N, typename T> friend class X;
    friend class X<1,P>;
};

template <typename T> class X<1,T> {
    X () { Y<T>::i; };     // access private field
};
-------------------------------------

gcc rejects the second friend declaration, apparently
refering to 14.7.3.17, which states that "An  explicit 
specialization declaration shall not be a friend 
declaration".

So I admit I am confused, both about the standard and
the behavior of different compilers. Since gcc behavior
restricts the use of the language (IMHO) unduly, I would
opt for it to follow the other compiler's interpretation.

Regards
  Wolfgang

Release:
unknown

Environment:
all gcc versions up to gcc3.2

------- Comment #1 From Kriang Lerdsuwanakij 2002-11-24 03:35 -------
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed.  14.5.3 p9 seems to be restricted to template
    friend declaration like
      template <typename P> friend class X<1,P>;
    not the ordinary
      friend class X<1,P>;

------- Comment #2 From Kriang Lerdsuwanakij 2002-11-24 03:37 -------
Responsible-Changed-From-To: unassigned->lerdsuwa
Responsible-Changed-Why: Working on friend issues.

------- Comment #3 From Kriang Lerdsuwanakij 2002-12-16 23:24 -------
From: Kriang Lerdsuwanakij <lerdsuwa@users.sourceforge.net>
To: gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org, lerdsuwa@gcc.gnu.org,
        bangerth@ticam.utexas.edu, gcc-bugs@gcc.gnu.org
Cc:  
Subject: Re: c++/8099: Friend classes and template specializations
Date: Mon, 16 Dec 2002 23:24:47 +0700

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8099
 
 I have submit a patch that fixes this.  It is waiting for approval:
 
    http://gcc.gnu.org/ml/gcc-patches/2002-11/msg01639.html
 
 --Kriang
 


------- Comment #4 From Kriang Lerdsuwanakij 2002-12-19 07:33 -------
State-Changed-From-To: analyzed->closed
State-Changed-Why: Fixed in 3.3 and main trunk.

------- Comment #5 From Kriang Lerdsuwanakij 2002-12-19 15:11 -------
From: lerdsuwa@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/8099
Date: 19 Dec 2002 15:11:30 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	lerdsuwa@gcc.gnu.org	2002-12-19 07:11:29
 
 Modified files:
 	gcc/cp         : ChangeLog friend.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/g++.dg/template: friend9.C 
 
 Log message:
 	PR c++/8099
 	* friend.c (make_friend_class): Allow partial specialization
 	when declaration is not a template friend.
 	
 	* g++.dg/template/friend9.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3083&r2=1.3084
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/friend.c.diff?cvsroot=gcc&r1=1.78&r2=1.79
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2266&r2=1.2267
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/friend9.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
 

------- Comment #6 From Kriang Lerdsuwanakij 2002-12-19 15:35 -------
From: lerdsuwa@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/8099
Date: 19 Dec 2002 15:35:52 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_3-branch
 Changes by:	lerdsuwa@gcc.gnu.org	2002-12-19 07:35:52
 
 Modified files:
 	gcc/cp         : ChangeLog friend.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/g++.dg/template: friend9.C 
 
 Log message:
 	PR c++/8099
 	* friend.c (make_friend_class): Allow partial specialization
 	when declaration is not a template friend.
 	
 	* g++.dg/template/friend9.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.3076.2.4&r2=1.3076.2.5
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/friend.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.77&r2=1.77.4.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.2&r2=1.2261.2.3
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/friend9.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1