Topic: Declaring template friends


Author: Venkat Jagadish <venkat@afmedia.com>
Date: 1998/11/05
Raw View
 Hello all,

    What is the correct syntax to declare a template class as a friend
to a non-template class
  and what are the compilers (w/ version) that support it ?


 I have tried the following :

       1.
template <class TYPE> class TemplateClass { };

  class NonTemplate_Class {
    template <class TYPE> friend class TemplateClass<TYPE>;
  };


       2.
template <class TYPE> class TemplateClass { };

  class NonTemplate_Class {
    template <class TYPE> friend class TemplateClass;
  };


 Both my Solaris 2.6  SparcWorks Workshop Compiler Version 4.2 and  g++
2.7.2 fail to compile.

 Specifically Workshop says:

    "friend_template1.cc", line 5: Error: Templates can only be declared
at the global level.
"friend_template1.cc", line 5: Error: Type name expected instead of
"TYPE".
"friend_template1.cc", line 5: Error: "," expected instead of "TYPE".
"friend_template1.cc", line 5: Error: TYPE is not defined.
"friend_template1.cc", line 5: Error: Illegal value for template
parameter.
"friend_template1.cc", line 5: Error: Too many arguments for template
TemplateClass.
6 Error(s) detected.

 for Case 1   and

"friend_template3.cc", line 4: Error: Templates can only be declared at
the global level.
"friend_template3.cc", line 4: Error: Multiple declaration for
TemplateClass.
2 Error(s) detected.


for Case 2.


g++ just says:

friend_template1.cc:5: parse error before `template'

in both cases which leads me to believe that 2.7.2 didnt support
templates at all.

Thanks for any help in advance .


V.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Sidney Steward <ssteward@erols.com>
Date: 1998/11/06
Raw View
Venkat Jagadish wrote:
>
>  Hello all,
>
>     What is the correct syntax to declare a template class as a friend
> to a non-template class
>   and what are the compilers (w/ version) that support it ?
>
>  I have tried the following :
(snip)

The following works fine on egcs (g++?) 2.91.57
(distributed with Cygwin b20 and running on my NT4):

#include<iostream>

class Beta {
 int m_num;
public:
 Beta() : m_num(4) {};
 template <class T> friend class Alpha;
};

template <class T> class Alpha {
public:
 T m_member;
 int f(Beta b) { return b.m_num; }
};

int main()
{
 Beta beth;
 Alpha< char > amber;
 cout << amber.f( beth );
 return 0;
}


Sidney
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]