Connecting Tech Pros Worldwide Help | Site Map

Friend Function in Template Class

Trevor Lango
Guest
 
Posts: n/a
#1: Jul 22 '05
What is the appropriate syntax for placing a friend function that includes
as one of it's parameters a pointer to the class object itself within the
template class?

I have the following:

//************************************************** **
// testClass.h
//************************************************** **
#ifndef TESTCLASS_H
#define TESTCLASS_H

template< class T >
class TestClass
{
friend void someFunction( const TestClass< T > *someTestClass );

public:
TestClass( );

};
#endif

//************************************************** **
// end testClass.h
//************************************************** **



//************************************************** **
// testClass.cpp
//************************************************** **
#include "testClass.h"

template< class T >
void someFunction< T >( const TestClass< T > *someTestClass )
{
}

template< class T >
TestClass< T >::TestClass( )
{
}

//************************************************** **
// end testClass.cpp
//************************************************** **


The following are the warnings/error(s) produced by the compiler:

U:\test>g++ -c testClass.cpp
In file included from testClass.cpp:1:
testClass.h:10: warning: friend declaration `void someFunction(const
TestClass<T> *)'
testClass.h:10: warning: declares a non-template function
testClass.h:10: warning: (if this is not what you intended, make sure
testClass.h:10: warning: the function template has already been declared,
testClass.h:10: warning: and add <> after the function name here)
testClass.h:10: warning: -Wno-non-template-friend disables this warning.
testClass.cpp:5: template-id `someFunction<T>' in declaration of primary
template
U:\test>

Following the compiler's suggestion removes the warning but still yields the
following error:

U:\test>g++ -Wno-non-template-friend -c testClass.cpp
testClass.cpp:5: template-id `someFunction<T>' in declaration of primary
template
U:\test>


Any suggestions/comments as to how to correct this will be much appreciated.
I am uncertain as to how to use friend functions correctly within template
classes, any clarification(s) are welcomed!


Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Friend Function in Template Class


"Trevor Lango" <tmlango@member.aiche.org> wrote...[color=blue]
> What is the appropriate syntax for placing a friend function that includes
> as one of it's parameters a pointer to the class object itself within the
> template class?
>
> I have the following:
>
> //************************************************** **
> // testClass.h
> //************************************************** **
> #ifndef TESTCLASS_H
> #define TESTCLASS_H
>
> template< class T >
> class TestClass
> {
> friend void someFunction( const TestClass< T > *someTestClass );[/color]

If your function is a template, you need to declare it before first
use. Otherwise this 'friend' declaration becomes a declaration of
a function that is not a template. IIRC.
[color=blue]
>
> public:
> TestClass( );
>
> };
> #endif
>
> //************************************************** **
> // end testClass.h
> //************************************************** **
>
>
>
> //************************************************** **
> // testClass.cpp
> //************************************************** **
> #include "testClass.h"
>
> template< class T >
> void someFunction< T >( const TestClass< T > *someTestClass )
> {
> }
>
> template< class T >
> TestClass< T >::TestClass( )
> {
> }
>
> //************************************************** **
> // end testClass.cpp
> //************************************************** **
>
>
> The following are the warnings/error(s) produced by the compiler:
>
> U:\test>g++ -c testClass.cpp
> In file included from testClass.cpp:1:
> testClass.h:10: warning: friend declaration `void someFunction(const
> TestClass<T> *)'
> testClass.h:10: warning: declares a non-template function[/color]

Exactly.
[color=blue]
> testClass.h:10: warning: (if this is not what you intended, make sure
> testClass.h:10: warning: the function template has already been[/color]
declared,[color=blue]
> testClass.h:10: warning: and add <> after the function name here)
> testClass.h:10: warning: -Wno-non-template-friend disables this warning.
> testClass.cpp:5: template-id `someFunction<T>' in declaration of primary
> template
> U:\test>
>
> Following the compiler's suggestion removes the warning but still yields[/color]
the[color=blue]
> following error:
>
> U:\test>g++ -Wno-non-template-friend -c testClass.cpp
> testClass.cpp:5: template-id `someFunction<T>' in declaration of primary
> template
> U:\test>
>
>
> Any suggestions/comments as to how to correct this will be much[/color]
appreciated.[color=blue]
> I am uncertain as to how to use friend functions correctly within template
> classes, any clarification(s) are welcomed![/color]

It's a bit tricky, and both declarations are better put in the same
header, to form this sequence:

template<class T> class TestClass; // forward declaration
template<class T> void someFunc(TestClass<T> const*); // func decl
template<class T> class TestClass
{
friend void someFunc<T>(TestClass const*); // now it uses the func
// declared above
};

...

Victor


Adam Fineman
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Friend Function in Template Class


Trevor Lango wrote:[color=blue]
> What is the appropriate syntax for placing a friend function that includes
> as one of it's parameters a pointer to the class object itself within the
> template class?
>
> I have the following:
>
> //************************************************** **
> // testClass.h
> //************************************************** **
> #ifndef TESTCLASS_H
> #define TESTCLASS_H
>
> template< class T >
> class TestClass
> {
> friend void someFunction( const TestClass< T > *someTestClass );[/color]

friend void someFunction<>( const TestClass< T > *someTestClass );
[color=blue]
>
> public:
> TestClass( );
>
> };
> #endif
>
> //************************************************** **
> // end testClass.h
> //************************************************** **
>
>
>
> //************************************************** **
> // testClass.cpp
> //************************************************** **
> #include "testClass.h"
>
> template< class T >
> void someFunction< T >( const TestClass< T > *someTestClass )[/color]

template< class T>
void someFunction( const TestClass< T > *someTestClass )
[color=blue]
> {
> }
>
>
> U:\test>g++ -c testClass.cpp
> In file included from testClass.cpp:1:
> testClass.h:10: warning: friend declaration `void someFunction(const
> TestClass<T> *)'
> testClass.h:10: warning: declares a non-template function
> testClass.h:10: warning: (if this is not what you intended, make sure
> testClass.h:10: warning: the function template has already been declared,
> testClass.h:10: warning: and add <> after the function name here)
> testClass.h:10: warning: -Wno-non-template-friend disables this warning.
> testClass.cpp:5: template-id `someFunction<T>' in declaration of primary
> template
> U:\test>
>
> Following the compiler's suggestion removes the warning but still yields the
> following error:
>
> U:\test>g++ -Wno-non-template-friend -c testClass.cpp
> testClass.cpp:5: template-id `someFunction<T>' in declaration of primary
> template
> U:\test>
>[/color]

Yes, you had fixed the first issue, but not the second.

HTH,

- Adam

--
Reverse domain name to reply.

Nick Hounsome
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Friend Function in Template Class



"Trevor Lango" <tmlango@member.aiche.org> wrote in message
news:bc_Kb.6711$zF5.1443@newssvr27.news.prodigy.co m...[color=blue]
> What is the appropriate syntax for placing a friend function that includes
> as one of it's parameters a pointer to the class object itself within the
> template class?
>
> I have the following:
>
> //************************************************** **
> // testClass.h
> //************************************************** **
> #ifndef TESTCLASS_H
> #define TESTCLASS_H
>
> template< class T >
> class TestClass
> {
> friend void someFunction( const TestClass< T > *someTestClass );[/color]

Why would you want such a thing in this context?
Unless you have missed out some extra args the smart thing to do is just
make it a simple const member function
void someFunction() const;


Trevor Lango
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Friend Function in Template Class



"Nick Hounsome" <nh002@blueyonder.co.uk> wrote in message
news:NUjLb.1182$qM1.315@news-binary.blueyonder.co.uk...[color=blue]
>
> "Trevor Lango" <tmlango@member.aiche.org> wrote in message
> news:bc_Kb.6711$zF5.1443@newssvr27.news.prodigy.co m...[color=green]
> > What is the appropriate syntax for placing a friend function that[/color][/color]
includes[color=blue][color=green]
> > as one of it's parameters a pointer to the class object itself within[/color][/color]
the[color=blue][color=green]
> > template class?
> >
> > I have the following:
> >
> > //************************************************** **
> > // testClass.h
> > //************************************************** **
> > #ifndef TESTCLASS_H
> > #define TESTCLASS_H
> >
> > template< class T >
> > class TestClass
> > {
> > friend void someFunction( const TestClass< T > *someTestClass );[/color]
>
> Why would you want such a thing in this context?
> Unless you have missed out some extra args the smart thing to do is just
> make it a simple const member function
> void someFunction() const;
>[/color]

The function in question is the following (which can't be a member
function):

friend ostream &operator<<( ostream &, const someClass * );


Nick Hounsome
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Friend Function in Template Class



"Trevor Lango" <tmlango@member.aiche.org> wrote in message
news:XimLb.8610$UC2.2217@newssvr25.news.prodigy.co m...[color=blue]
>
> "Nick Hounsome" <nh002@blueyonder.co.uk> wrote in message
> news:NUjLb.1182$qM1.315@news-binary.blueyonder.co.uk...[color=green]
> >
> > "Trevor Lango" <tmlango@member.aiche.org> wrote in message
> > news:bc_Kb.6711$zF5.1443@newssvr27.news.prodigy.co m...[color=darkred]
> > > What is the appropriate syntax for placing a friend function that[/color][/color]
> includes[color=green][color=darkred]
> > > as one of it's parameters a pointer to the class object itself within[/color][/color]
> the[color=green][color=darkred]
> > > template class?
> > >
> > > I have the following:
> > >
> > > //************************************************** **
> > > // testClass.h
> > > //************************************************** **
> > > #ifndef TESTCLASS_H
> > > #define TESTCLASS_H
> > >
> > > template< class T >
> > > class TestClass
> > > {
> > > friend void someFunction( const TestClass< T > *someTestClass );[/color]
> >
> > Why would you want such a thing in this context?
> > Unless you have missed out some extra args the smart thing to do is just
> > make it a simple const member function
> > void someFunction() const;
> >[/color]
>
> The function in question is the following (which can't be a member
> function):
>
> friend ostream &operator<<( ostream &, const someClass * );[/color]

This is the third time in 2 days that some version of this has come up and I
would do them all the same:

template <class T> class TestClass {
public: ostream& print(ostream&) const; }

inline template <class T> ostream& operator<<(ostream& os, const
TestClass<T>& t)
{ return t.print(os); }

P.S. It is never a good idea to wite an output operator that takes a
pointer - always use a const reference.

The other advantage of this approach is that just adding 'virtual' gives you
polymorphic printing.

I am unconvinced of the need for any sort of special access for printing
anyway because
I think it is questionable to output some aspect of an object that you
couldn't get from the public interface.
This is not a cast iron rule with me but consider carefully why you would
break it.



Closed Thread