home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 17896 < prev    next >
Encoding:
Text File  |  1992-12-13  |  2.3 KB  |  60 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!psinntp!newstand.syr.edu!rodan.acs.syr.edu!krnorton
  3. From: krnorton@rodan.acs.syr.edu (Karen R. Norton)
  4. Subject: Question Re. Integer Template Arguments
  5. Message-ID: <1992Dec12.175103.23139@newstand.syr.edu>
  6. Organization: Syracuse University, Syracuse, NY
  7. Date: Sat, 12 Dec 92 17:51:03 EST
  8. Lines: 50
  9.  
  10.  
  11. What I would like to do is something like the following:
  12. ------------------------------------------------------------------------
  13. template <int i> class C {
  14.   ....
  15. };
  16.  
  17. template <int i1, int i2> C<i1+i2> f(C<i1> c1, C<i2> c2) {
  18.   return C<i1+i2>(....);    //Call constructor with some args.
  19. }
  20. ------------------------------------------------------------------------
  21. Unfortunately even tho' this works with g++ 2.3.2, the ARM expressly
  22. forbids this (pg. 347 - "All template-args for a function template must be
  23. type-arguments.").
  24.  
  25. An attempted work-around:
  26. ------------------------------------------------------------------------
  27. template <int i> class C {
  28. public: enum {D= i}; ....
  29. };
  30.  
  31. template <class C1, class C2> C<C1::D + C2::D> f(C1 c1, C2 c2) {
  32.   return C<C1::D + C2::D>(....);    //Call constructor with some args.
  33. }
  34. ------------------------------------------------------------------------
  35. Unfortunately, g++ 2.3.2 gives an error when compiling the return type for
  36. f() - it claims that C1 and C2 are not aggregates.  It has no problems with
  37. the similar construct used in the constructor in the return value (I proved
  38. this by instantiating the return type manually, while leaving the return
  39. value expression unchanged).
  40.  
  41. I have the following questions:
  42.  
  43. 1.  What is the reason for the restriction on template-args for function
  44. templates?  The rationale presented in the ARM appears to be too weak a
  45. reason for such a strong restriction.  The restriction given on pg. 280 of
  46. Stroustrup2 ("Each template argument of a function template must affect the
  47. type of the function by affecting at least one argument type of functions
  48. generated from the template.") appears much more reasonable than the
  49. restriction given in the ARM. 
  50.  
  51. 2.  Is my work-around incorrect C++, or does g++ have a problem?
  52.  
  53. [If replying by email, please use the following email address, as I am
  54. posting from a friend's account on another machine].
  55.  
  56. Thank you.
  57.  
  58. Zerksis D. Umrigar
  59. (umrigar@bingsuns.cc.binghamton.edu)
  60.