Article View: comp.std.c++
Article #4106Re: Better template syntax?
From: Roman Lechtchinsky
Date: Thu, 18 Apr 1996 21:35
Date: Thu, 18 Apr 1996 21:35
74 lines
3537 bytes
3537 bytes
Rob Stewart wrote:
>
> Roman Lechtchinsky wrote:
> >
> > Hi,
> >
> > sorry for being curious but I'm only human... My question is: when declaring
> > templates, why isn't it required to declare what one expects of the
> > template's parameters? Let's create a template function:
> >
> > template<class T> bool equal( const T& t1, const T& t2 )
> > {
> > return t1==t2;
> > }
> >
> > Now, personally I would prefer to write something like:
> >
> > template<class T { bool operator==( const T& t ) const; }> ...
> >[...]
>
> I can't answer whether this has been considered (or rejected),
Yes, it has been. I forgot that D&E contains a ( rather short ) discussion on
this topic. Probably I should have written: "Has there been an attempt to
define this", a question still unanswered.
> but I can see a problem with your proposal. What if a template
> required ten, fifteen, twenty mfs in its parameterizing type?
> What if those mfs have many parameters? The resulting list of
> mfs and parameters would become quite ungainly.
Actually, when I define a template I usually describe what it expects from
its parameters in a comment. This means I have to write this list anyway. Do
other programmers do it?
> Perhaps you could declare a class with the exact interface you
> expect and provide that as a model for the parameterizing type?
> This still leaves issues like equivalence. Are implicit
> conversion allowed in mfs of a parameterizing type in order to
> match the model? At what point do you decide there is no match?
I'm currently trying to define how this can be handled. In general, I think
the following would do: a function matches a function required by the
template if
a) its return type can be implicitly converted to the required return type
and
b) the types of its parameters can be implicitly converted to the types of
the correspondenting required parameters.
I wonder if someone has already tried this and what the results are.
>
> What about the template code? What if it you misuse the
> parameterizing type relative to your model? Is the model wrong
> or is your code wrong?
>
I'm not sure what you mean here.
> There's probably a lot more I haven't thought of yet.
Yes, definitely. However, there's probably a lot more that hasn't been
thought of ( or that is being thought of a this very moment ;-) about
templates as well.
Bye
Roman
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Technical Details
Message-ID:
<3176A9E8.555D@cs.tu-berlin.de>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageReferences:
<31741E6C.53CA@cs.tu-berlin.de> <317673A9.2EBB@datalytics.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: Roman Lechtchinsky <wolfro@cs.tu-berlin.de> Newsgroups: comp.std.c++ Subject: Re: Better template syntax? Date: 18 Apr 1996 21:35:05 GMT Organization: Technical University of Berlin Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <3176A9E8.555D@cs.tu-berlin.de> References: <31741E6C.53CA@cs.tu-berlin.de> <317673A9.2EBB@datalytics.com> NNTP-Posting-Host: taumet.eng.sun.com Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Nntp-Posting-Host: 130.149.17.233 X-Mailer: Mozilla 2.0 (Win95; I) X-Lines: 67 Content-Length: 2390 Originator: clamage@taumet