Post #100,379
5/5/03 3:29:08 PM
8/21/07 6:33:56 AM
|
Return value problem
you nailed it - how do you tell which type, T1 or T2 should be the return value? And how do you write the code such that the correct precision is maintained in the result?
Answer: you specialize the template - to the point that you've fully specified all possible overload sequences and then the template has shown itself to be worthless in this case (except for the everything-is-a-T case).
you could try this:
template<class T> double max(double x1, T x2) // if there's one double they're all double { return x1 > x2 ? x1 : x2; }
to save some work - but you'll get an ambiguity if you try max(double, double) between the max is all T's and max T1 T2 definitions.
Thus, C++ can help you expand work to fill all available time with no trouble at all.
Or you could use a macro.
"Packed like lemmings into shiny metal boxes. Contestants in a suicidal race." - Synchronicity II - The Police
Return value problem
you nailed it - how do you tell which type, T1 or T2 should be the return value? And how do you write the code such that the correct precision is maintained in the result?
Answer: you specialize the template - to the point that you've fully specified all possible overload sequences and then the template has shown itself to be worthless in this case (except for the everything-is-a-T case).
you could try this:
template<class T> double max(double x1, T x2) // if there's one double they're all double { return x1 > x2 ? x1 : x2; }
to save some work - but you'll get an ambiguity if you try max(double, double) between the max is all T's and max T1 T2 definitions.
Thus, C++ can help you expand work to fill all available time with no trouble at all.
Or you could use a macro.
"Packed like lemmings into shiny metal boxes. Contestants in a suicidal race." - Synchronicity II - The Police
|
Post #100,408
5/5/03 7:30:55 PM
|
I'll take a look for the solution for ret value
In the meantime, have a fill of this: a guy at the place where I work uses templates to overload _integers_ with tracking code. He will then use those overloaded integers in the contexts wher he wants to collect stats. For the user, it's still an integer, slightly larger in memory. Stats collector has a thread to get snapshots and remember them.
This struck me as a very Smalltalk-ish thing to do. :)
--
Want to be original? Start with a great template!
|
Post #100,450
5/5/03 10:19:41 PM
|
Re: Return value problem
It's interesting - I've done most of my C++ coding in some numerical/mathematical context - needless to say, the occasion to use templates came up again and again - and then I'd think - no, the very fact that I'm thinking about this means these things are enough different to treat differently, and so forget this stupid template.
See, it's not really a template - it's a second, backdoor attempt to have a scalable type of data, since the objectness in the front door effort is a sort of fake.
Finally I'd accomplish making the differences manifest with overloading by hand - which no doubt is all the template is doing in the first place, and the code made much more sense.
-drl
|
Post #100,517
5/6/03 9:17:21 AM
|
In the end
you do need different implementations for different types, so "overloading by hand" is inevitable. It's nice to have compiler do it for you.
I agree that templates represent another, backdoor attempt at real type system, after virtual functions failed to deliver. I am beginning to think that templates are simply Smalltalk-style metadata, represented in many, many versions of generated code. Just like in Lisp one can implement lists with closures, the same way one can replace metadata with lots of generated code. To some extent. There have to be better statically-typed languages.
--
Want to be original? Start with a great template!
|
Post #100,689
5/7/03 5:13:57 AM
|
A better statically typed language - have you tried Haskell?
"good ideas and bad code build communities, the other three combinations do not" - [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
|
Post #100,949
5/8/03 3:15:36 PM
8/21/07 12:41:27 PM
|
Read the tutorials
it is somewhat related to the ML series IIRC, the most popular being OCAML I think.
There are some interesting ideas in there and I can think of two language research projects that are also working on similar typing ideas - one called slate which is being implemented in squeak, and the needle project.
The thing that keeps me from bothering to write code in languages like these is lack of platform interface support - ie, is there a bridge to most system routines I might want or a reasonably complete library availble. (for instance oberon was touted as being a great oo environment for awhile - but it had its own everything and always looke really ugly and alien on all the platforms I've seen it run on).
"Packed like lemmings into shiny metal boxes. Contestants in a suicidal race." - Synchronicity II - The Police
|
Post #100,998
5/8/03 7:32:39 PM
|
Today's link at /.
An [link|http://www.paulgraham.com/hp.html|article] on "hackers".
>>>>>>>>>>>>>>>>>>> We need a language that lets us scribble and smudge and smear, not a language where you have to sit with a teacup of types balanced on your knee and make polite conversation with a strict old aunt of a compiler. <<<<<<<<<<<<<<<<<<<
--
Want to be original? Start with a great template!
|