Subject: problem with friend function
From: Holger Burkarth <[email-masked]>
To: [email-masked]
Date: Sat, 22 May 1999 08:29:11 +0100

/*
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
[email-masked]
http://www.prodad.de

--
Want to unsubscribe from this list?
Send a message to [email-masked]