This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

problem with friend function


/*
Hello,

I have a question to the c++ gurus.
Why do not work the friend function ?
A friend function is in the same scope as a global
declared function - or not ?

The sample shows the problem by use Z<>::Compare() as friend.
The define USE_FRIEND enables the friend-test.
When define USE_FRIEND=1, the compiler ignores the
Z<>::Compare() definition and prints errors.

Note:

The compiler must create function W<>::Cmp as a real function,
for referencing the pointer of the function -> constructor of W<>.
When I remove this line, all works fine.


What's the reason ?

Thanks in advance.
*/


extern void set( int(*)(...) );

//#define USE_FRIEND 1


template<class T>
int Compare(const T& a,const T& b)
{
  if(a < b) return -1;
  if(a > b) return  1;
  return 0;
}





template<class T>
struct Z
{
  T Get() const;

#if USE_FRIEND != 0
  friend int Compare(const Z<T>& k1,const Z<T>& k2)
    { return Compare( k1.Get(), k2.Get() ); };
#endif
};

#if USE_FRIEND == 0

template<class T>
int Compare(const Z<T>& k1,const Z<T>& k2)
    { return Compare( k1.Get(), k2.Get() ); };

#endif







template<class T>
struct W
{
  W() { set( ( int(*)(...) )Cmp  ); }

  static int Cmp(const T* k1,const T* k2)
    { return Compare(*k1,*k2); }
};




void func()
{
  W< Z<int> > w;

  (void)w;
}



/*
$ g++ main.cpp -D USE_FRIEND=1

main.cpp: In function `int Compare<Z<int>>(const struct Z<int> &, const struct
Z<int> &)':
main.cpp:65:   instantiated from here
main.cpp:25: no match for `const Z<int> & < const Z<int> &'
main.cpp:26: no match for `const Z<int> & > const Z<int> &'

 gcc version egcs-2.91.57 19980901 (egcs-1.1 release)

*/

--
Holger Burkarth
Software-Developer
burkarth@prodad.de
http://www.prodad.de


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]