home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!gossip.pyramid.com!decwrl!borland.com!pete
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c++
- Subject: Re: Template-instantiation fails with references
- Message-ID: <1993Jan5.182315.5087@borland.com>
- Date: 5 Jan 93 18:23:15 GMT
- References: <stefans.726234949@bauv106>
- Sender: news@borland.com (News Admin)
- Organization: Borland International
- Lines: 47
- Originator: pete@genghis.borland.com
-
- In article <stefans.726234949@bauv106> stefans@bauv.unibw-muenchen.de (Stefan Schwarz) writes:
- >Hi all,
- >
- >just came to a serious problem with templates.
-
- One useful technique for understanding problems with templates is to
- write the "same" code without using a template.
-
- >Simple code:
- >
- >#include <iostream.h>
- >template <class T> class Collection
- >{
- >private:
- > T *_buffer;
- > T _endtag;
- >public:
- > Collection (size_t size, T endtag) {
- > _buffer = new T [size];
- > _endtag = endtag; }
- >};
- >
- >class Foo
- >{
- >};
- >
-
- class FooRefCollection
- {
- private:
- Foo& *_buffer;
- Foo& _endtag;
- public:
- FooRefCollection( size_t size, Foo& endtag ) {
- buffer = new Foo&[size];
- _endtag = endtag;
- }
- };
-
- This should produce the same error messages. There are two problems
- here. First, a reference must have an initializer. _endtag does not have an
- initializer, only an assignment to it. Second, it is not possible to create
- an array of Foo&.
- The first problem can be solved by initializing _endtag in the
- initializer list ( FooRefCollection( Foo& endtag ) : _endtag(endtag) {}
- The second can't be solved without redesigning the class.
- -- Pete
-