home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18715 < prev    next >
Encoding:
Text File  |  1993-01-05  |  1.6 KB  |  60 lines

  1. Path: sparky!uunet!olivea!gossip.pyramid.com!decwrl!borland.com!pete
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Template-instantiation fails with references
  5. Message-ID: <1993Jan5.182315.5087@borland.com>
  6. Date: 5 Jan 93 18:23:15 GMT
  7. References: <stefans.726234949@bauv106>
  8. Sender: news@borland.com (News Admin)
  9. Organization: Borland International
  10. Lines: 47
  11. Originator: pete@genghis.borland.com
  12.  
  13. In article <stefans.726234949@bauv106> stefans@bauv.unibw-muenchen.de (Stefan Schwarz) writes:
  14. >Hi all,
  15. >
  16. >just came to a serious problem with templates. 
  17.  
  18.     One useful technique for understanding problems with templates is to
  19. write the "same" code without using a template.
  20.  
  21. >Simple code:
  22. >
  23. >#include <iostream.h>
  24. >template <class T> class Collection
  25. >{
  26. >private:
  27. >        T *_buffer;
  28. >        T _endtag;
  29. >public:
  30. >        Collection (size_t size, T endtag) {
  31. >                _buffer = new T [size];
  32. >                _endtag = endtag; }
  33. >};
  34. >
  35. >class Foo
  36. >{
  37. >};
  38. >
  39.  
  40.     class FooRefCollection
  41.     {
  42.     private:
  43.         Foo& *_buffer;
  44.         Foo& _endtag;
  45.     public:
  46.         FooRefCollection( size_t size, Foo& endtag ) {
  47.             buffer = new Foo&[size];
  48.             _endtag = endtag;
  49.         }
  50.     };
  51.  
  52.     This should produce the same error messages.  There are two problems
  53. here.  First, a reference must have an initializer.  _endtag does not have an
  54. initializer, only an assignment to it.  Second, it is not possible to create
  55. an array of Foo&.
  56.     The first problem can be solved by initializing _endtag in the
  57. initializer list (  FooRefCollection( Foo& endtag ) : _endtag(endtag) {}
  58.     The second can't be solved without redesigning the class.
  59.     -- Pete
  60.