template friends

template friends

Post by Serg » Sun, 07 Apr 2002 12:47:40



Does anybody know to specify friends of the template class .
The syntax from C++ Primer, and Strooustrup's book doesn't work
- Linker error. can't find def
(I use BC++ 5.5)
And does BC++ support compilation of templates in separate file using
"export"?
This doesn't work, adding extra <> either.

template class <T>
class X
{
..
    friend void f(X<T>&);
..

Quote:};

template <class A>
void f(A& a) { ...}

--
Best regards,
Sergo.

 
 
 

template friends

Post by Mike Wahle » Sun, 07 Apr 2002 13:36:19



Quote:> Does anybody know to specify friends of the template class .
> The syntax from C++ Primer, and Strooustrup's book doesn't work
> - Linker error. can't find def
> (I use BC++ 5.5)
> And does BC++ support compilation of templates in separate file using
> "export"?

No, nor does any currently available C++ compiler I know of.

Quote:> This doesn't work, adding extra <> either.

> template class <T>
> class X
> {
> ..

template <typename T>

Quote:>     friend void f(X<T>&);
> ..
> };

> template <class A>
> void f(A& a) { ...}

You need to keep the declaration and definition in the
same source file.

-Mike

 
 
 

template friends

Post by Nithyanandham » Sun, 07 Apr 2002 13:33:38



> > Does anybody know to specify friends of the template class .
> > The syntax from C++ Primer, and Strooustrup's book doesn't work
> > - Linker error. can't find def
> > (I use BC++ 5.5)
> > And does BC++ support compilation of templates in separate file using
> > "export"?

> No, nor does any currently available C++ compiler I know of.

But, I guess Dinkumware has implemented it.

--

Nithyanand.
Siemens, Bangalore, India.
(Opinions expressed are my own and do not reflect the opinions of my
employer, SIEMENS)

 
 
 

template friends

Post by Bart Kowalsk » Sun, 07 Apr 2002 14:07:18





> > Does anybody know to specify friends of the template class .
> > The syntax from C++ Primer, and Strooustrup's book doesn't work
> > - Linker error. can't find def
> > (I use BC++ 5.5)
> > And does BC++ support compilation of templates in separate file using
> > "export"?

> No, nor does any currently available C++ compiler I know of.

Comeau C++ has implemented it, although it's still in beta.

Bart.

 
 
 

template friends

Post by Mike Wahle » Sun, 07 Apr 2002 14:15:00




> > > Does anybody know to specify friends of the template class .
> > > The syntax from C++ Primer, and Strooustrup's book doesn't work
> > > - Linker error. can't find def
> > > (I use BC++ 5.5)
> > > And does BC++ support compilation of templates in separate file using
> > > "export"?

> > No, nor does any currently available C++ compiler I know of.

> But, I guess Dinkumware has implemented it.

Not that I'm aware of.  'export' is a language keyword.
Dinkumware implements the standard library, not the
language.

-Mike

 
 
 

template friends

Post by Rakesh Tiwar » Sun, 07 Apr 2002 15:30:07



> Does anybody know to specify friends of the template class .
> The syntax from C++ Primer, and Strooustrup's book doesn't work
> - Linker error. can't find def
> (I use BC++ 5.5)
> And does BC++ support compilation of templates in separate file using
> "export"?
> This doesn't work, adding extra <> either.

> template class <T>
> class X
> {
> ..
>     friend void f(X<T>&);
> ..
> };

> template <class A>
> void f(A& a) { ...}

This compiles and links with gcc. May be you are missing a < T > after f
and before "(" in your freind declaration ?

template < class T>
class X
{
    friend void f< T >(X< T > &);

Quote:};

template < class A >
void f(A& a) { }

int main(void)
{
    int i = 1;
    f(i);
    return 0;

Quote:}

Regards,
Rakesh :-)
 
 
 

template friends

Post by Robert W Han » Sun, 07 Apr 2002 20:41:59


On Sat, 06 Apr 2002 06:30:07 GMT, Rakesh Tiwari


>This compiles and links with gcc. May be you are missing a < T > after f
>and before "(" in your freind declaration ?

>template < class T>
>class X
>{
>    friend void f< T >(X< T > &);
>};

>template < class A >
>void f(A& a) { }

>int main(void)
>{
>    int i = 1;
>    f(i);

I'll bet that it is calling the non-friend.  Try adding something
private to X and accessing it in f.  Or just move the function
definition below main.

Best wishes,

Bob

 
 
 

template friends

Post by nmtop4 » Mon, 08 Apr 2002 05:28:20


Quote:> This compiles and links with gcc. May be you are missing a < T > after f
> and before "(" in your freind declaration ?

> template < class T>
> class X
> {
>     friend void f< T >(X< T > &);
> };

> template < class A >
> void f(A& a) { }

> int main(void)
> {
>     int i = 1;
>     f(i);
>     return 0;
> }

Your example is irrelevant. You would need this:

#include <iostream>

template < class T >
class X
{
   friend void f< T >( X< T > &);

    private:

   T m_t;

   public:
      void Set( const T& t )
      {
         m_t = t;
      }

Quote:};

// A can be any class that has a member m_t
// which can be of any type that can be output to ostream

template < class A >
f( A& a )
{
    std::cout << a.m_t << std::endl;

Quote:}

int main()
{
   X< int > x;
   x.Set( 14 (l
   f( x );
   return 0;

- Show quoted text -

Quote:}

 
 
 

template friends

Post by Serg » Mon, 08 Apr 2002 14:46:37


This  syntax works on GCC 2.95

#include <windows.h>
#include <fstream>

ofstream out("t.txt");

template < class T >
class X
{
   T m_t;
 friend void f<>( X< T > &);
  public:
 void Set( const T& t )
      {    m_t = t;      }

Quote:};

template < class A >
void f( A& a )
{
    a.m_t = 10;
    out << a.m_t << std::endl;

Quote:}

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR
lpszArgument, int nFunsterStil)

{
   X< int > x;
   x.Set( 14);
   f( x );
   return 0;

Quote:}

 
 
 

template friends

Post by Serg » Mon, 08 Apr 2002 15:43:09


BCC++ also works with  such syntax: (thnx to Graeme Prentice

 template <class T>  void f1( X< T > & xx) { T datum=4 ; xx.member(datum);
.......}

 template <class T>  void f(  T &) { T.a=5;..........}

class X
{
int a;
friend void f1< >( X< T > &);
void member(const Data& w); ...

Quote:};

but requires either declaration or definition of a friend function BEFORE
defining the class
which friend it is. Also with defining the functions outside the class
typedefs in the class  require to use fully qualfied name. :(


Quote:> > This compiles and links with gcc. May be you are missing a < T > after f
> > and before "(" in your freind declaration ?

> > template < class T>
> > class X
> > {
> >     friend void f< T >(X< T > &);
> > };

> > template < class A >
> > void f(A& a) { }

> > int main(void)
> > {
> >     int i = 1;
> >     f(i);
> >     return 0;
> > }

> Your example is irrelevant. You would need this:

> #include <iostream>

> template < class T >
> class X
> {
>    friend void f< T >( X< T > &);

>     private:

>    T m_t;

>    public:
>       void Set( const T& t )
>       {
>          m_t = t;
>       }

> };

> // A can be any class that has a member m_t
> // which can be of any type that can be output to ostream

> template < class A >
> f( A& a )
> {
>     std::cout << a.m_t << std::endl;
> }

> int main()
> {
>    X< int > x;
>    x.Set( 14 (l
>    f( x );
>    return 0;
> }

 
 
 

1. friend templates and template friends

Hi,
        I have been looking everywhere, and can't find a solution to
this: How should a class template be declared as a friend?
1. template <class T> friend class A<T>;
2. friend template <class T>  class A<T>;

None of them work in gcc 2.8.0, which I have been using.

JJ

--
JJ Merelo                         | http://kal-el.ugr.es/htbin/jj-plex
Grupo Geneura ---- Univ. Granada  | http://kal-el.ugr.es/


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. SID enthusiasts: I need some help...

3. template friend function of a template class

4. Useful programs (where are they??)

5. Template friends in class templates.

6. Java HORB & CORBA

7. Template friends of template classes

8. Prompt

9. template friends

10. Q: template friend support in VC++ 7 (.NET)

11. non-template-friend

12. Templates, friend classes and namespace trouble

13. Template friend function