From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id SAA07698; Mon, 27 Sep 2004 18:38:10 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA06771 for ; Mon, 27 Sep 2004 18:38:08 +0200 (MET DST) Received: from smtp1.adl2.internode.on.net (smtp1.adl2.internode.on.net [203.16.214.181]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id i8RGc56S003510 for ; Mon, 27 Sep 2004 18:38:07 +0200 Received: from [192.168.1.200] (ppp202-133.lns1.syd3.internode.on.net [203.122.202.133]) by smtp1.adl2.internode.on.net (8.12.9/8.12.9) with ESMTP id i8RGc14Y020781; Tue, 28 Sep 2004 02:08:02 +0930 (CST) Subject: Re: [Caml-list] C++ STL and template features compared with OCaml parametric polymorphism and OO features From: skaller Reply-To: skaller@users.sourceforge.net To: Brian Hurt Cc: caml-list In-Reply-To: References: Content-Type: text/plain Message-Id: <1096303081.28613.710.camel@pelican.wigram> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-4) Date: 28 Sep 2004 02:38:01 +1000 Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 415841ED.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 sourceforge:01 2004:99 foo:01 foo:01 pointers:01 generic:01 passing:01 superclass:01 templating:01 struct:01 const:01 ,1.0:01 ,1.0:01 pointers:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tue, 2004-09-28 at 01:30, Brian Hurt wrote: > On 28 Sep 2004, skaller wrote: > > For templates all you need is a class with an operator()() method. > > > > Dynamic dispatch is only needed if you need > > run time function variables (first class functions). > > All this means is that the calling code, instead of calling foo->doit(), > now instead calls (*foo)(). Not that big of a difference in coding > volume. The call is done by foo() which works for C functions, pointers to them, and function objects. This is required for STL to be generic. > And you still need dynamic dispatch because you're passing the superclass > type in. Type information is not required, the C++ compiler checks the calls argument and return types after instantiation. > Unless you're talking about templating the map/fold functions so > that you get a different instantiation for each call? Yes, because it costs nothing, which is much cheaper than the virtual dispatch: template T apply(F f, T a) { return f(a); } struct Sin { float operator()(float a) const { return a; } }; float (*pf)(float)= sin; apply(sin,1.0); apply(Sin(),1.0); apply(pf,1.0); No dynamic dispatch is required here. The templates works with C functions, pointers to C functions, and function objects. The first two calls should both resolve to just sin(1.0) (the call through the C pointer requires an extra memory access .. :) If you use a base and dynamic dispatch you'd incur an overhead. Note the call is applied to a function constant argument (the sine function). Felix only uses dynamic dispatch when the function is stored in a variable (or when it is too stupid to optimise the call away by substitution). BTW: note in the template, F is NOT the type of the function -- it is the *class* of the function. The actual function has type T -> T The class of the function is irrelevant. The class can even be float (*)(float), it is still irrelavant -- what matters is the type of f(a). For the function object, that's a completely distinct method with its own type -- unrelated to the type of F: F is used solely to lookup operator()() -- oh yeah, that's ad hoc polymorphism.. :) -- John Skaller, mailto:skaller@users.sf.net voice: 061-2-9660-0850, snail: PO BOX 401 Glebe NSW 2037 Australia Checkout the Felix programming language http://felix.sf.net ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners