home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / gnu / g / bug / 2172 < prev    next >
Encoding:
Text File  |  1993-01-08  |  3.5 KB  |  115 lines

  1. Newsgroups: gnu.g++.bug
  2. Path: sparky!uunet!cis.ohio-state.edu!pic.ucla.edu!jimc
  3. From: jimc@pic.ucla.edu
  4. Subject: Much trouble with pointer to function
  5. Message-ID: <9301072001.AA06888@malibu.pic.ucla.edu>
  6. Sender: gnulists@ai.mit.edu
  7. Organization: GNUs Not Usenet
  8. Distribution: gnu
  9. Date: Thu, 7 Jan 1993 04:01:11 GMT
  10. Approved: bug-g++@prep.ai.mit.edu
  11. Lines: 102
  12.  
  13. Gnu g++ v2.3.3 for Sparc-SunOS-4.1.1
  14.  
  15. A template function has an argument which is a pointer-to-function that
  16. takes arguments of the template class.  No amount of wriggling will get
  17. a call to this function to compile.  The complaint is "type unification
  18. failed for function template `xxx'".  Relevant issues are:
  19.  
  20. 1.  Can the compiler recognize that anything (e.g. an explicitly typed
  21. pointer-to-function) matches the template function pointer argument? 
  22. The internal error 180 in this case has been reported separately.
  23.  
  24. 2.  When a template function name is the argument, can the compiler
  25. recursively determine that it's capable of generating an instance which
  26. will match the required argument type?  (And what does the present draft
  27. standard say about which recursions the compiler is required or forbidden
  28. to do?  If the template function args use a typedef defined within the
  29. template arg class, it's "not of aggregate type", e.g.:
  30.  
  31.     template <class T> struct gorf {
  32.         typedef whatever BASE;
  33.         etc.
  34.     };
  35.     template <class G> testfcn(G g, G::BASE thing) //Rejects G::BASE
  36.     template <class T> testfcn(gorf<T> g, gorf<T>::BASE thing) //Rejected
  37.  
  38. 3.  Probably related: what's being complained about in the warning
  39. "contravariance violation for method types ignored"?
  40.  
  41. James F. Carter        (213) 825-2897
  42. UCLA-Mathnet;  6221 MSA; 405 Hilgard Ave.; Los Angeles, CA, USA  90024-1555
  43. Internet: jimc@math.ucla.edu            BITNET: jimc%math.ucla.edu@INTERBIT
  44. UUCP:...!{ucsd,ames,ncar,gatech,purdue,rutgers,decvax,uunet}!math.ucla.edu!jimc
  45.  
  46.  
  47. -------- Source file
  48.  
  49. //  bug19.C -- type unification failed for pointer-to-function template
  50.  
  51. #include <stdlib.h>
  52. #include <iostream.h>
  53.  
  54. template<class T>
  55. class data {
  56.     int siz;
  57.     T* stuff;
  58.   public:
  59.     data(int n) : siz(n), stuff(new T[n]) { }
  60.     ~data()
  61.     { delete stuff; }
  62.     int dim() const
  63.     { return siz; }
  64.     const T& operator [] (int i) const
  65.     { return stuff[i]; }
  66.     T& operator [] (int i)
  67.     { return stuff[i]; }
  68.     static int compare(T* a, T* b)        //Nasty surprise if T=double
  69.     { return (int)(*a - *b); }
  70.     friend ostream& operator << (ostream& f, const data<T>& d);
  71.     void sortme()
  72.     { qsort(stuff, dim(), sizeof(T), compare); }
  73. //kab.C:26: warning: contravariance violation for method types ignored
  74. // In stdlib.h: void qsort(void*, size_t, size_t, auto int (*ptf)(void*,void*));
  75. };
  76.  
  77. template <class T> ostream& operator << (ostream& f, const data<T>& d) {
  78.     for (int i = 0; i < d.dim(); ++i)
  79.     f << d[i] << ' ';
  80.     return f;
  81. }
  82.     
  83. template<class T>
  84. int compare(T* a, T* b){ return *a - *b; }
  85.  
  86. template<class T>
  87. void sort( data<T>d, int (*compare)(T*,T*) )
  88.      qsort(&(d[0]), d.dim(), sizeof(T), compare); 
  89. }
  90.  
  91. main(){
  92.     data<int> d(5);
  93.     for (int i = 0; i < d.dim(); ++i)
  94.         d[i] = d.dim() - i;
  95.     cout << "Before\t" << d << '\n';
  96. //    d.sortme();    // Works
  97.  
  98. //    sort(d, compare);
  99. //         type unification failed for function template `sort'
  100.  
  101. //    int (*cptr)(int*,int*) = compare;    //Gets internal err 180
  102. //    sort (d, cptr);
  103.  
  104. //    sort(d, d.compare);
  105. //         type unification failed for function template `sort'
  106.  
  107.     int (*cptr)(int*,int*) = d.compare;    //Initialization works
  108.     sort (d, cptr);
  109. //         type unification failed for function template `sort'
  110.  
  111.     cout << "After\t" << d << '\n';
  112. }
  113.  
  114.