delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/05/22/02:26:20

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
From: Holger Burkarth <burkarth AT prodad DOT de>
Reply-To: Holger Burkarth <burkarth AT prodad DOT de>
To: cygwin AT sourceware DOT cygnus DOT com
Date: Sat, 22 May 1999 08:29:11 +0100
Message-ID: <yam7811.584.136257320@mail.toplink.net>
X-Mailer: YAM 1.3.5 [040] - Amiga Mailer by Marcel Beck
Organization: proDAD-Software
Subject: problem with friend function
MIME-Version: 1.0

/*
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 AT prodad DOT de
http://www.prodad.de


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019