POV-Ray : Newsgroups : povray.off-topic : Standard libraries : Re: Standard libraries Server Time
6 Sep 2024 11:19:36 EDT (-0400)
  Re: Standard libraries  
From: Warp
Date: 5 Mar 2009 20:40:01
Message: <49b07ef1@news.povray.org>
Nicolas Alvarez <nic### [at] gmailcom> wrote:
> Invisible wrote:
> > You appreciate them *now*... Wait until you start using a language where
> > the standard way to iterate over a data structure is to pass your "loop
> > body" (as a lambda function argument) to the function that does the
> > traversal. :-P

> C++0x?

  It will indeed introduce core features which help a lot in this matter.

  For example the current standard library has a for_each() utility
function which in theory makes it easier to loop through an iterator
range and apply some function to each element. However, in practice
doing this is so cumbersome that few people prefer that over the more
traditional for loop. The reason is that there's no way in the current
C++ to embed the function into the for_each() call. Instead, you have
to write the function separately and give the name to for_each(). Great
if you already have such a function, but more trouble than it's worth
if you don't, which means you have to write the function.

  Also even if you go through the trouble of writing the function, it
still has its limits. Most prominently, it's not a closure, which means
that the function has no access to the variables in the scope where the
for_each() is executed. This reduces the usability of for_each() even
further, compared to the traditional for loop (where there rather
obviously *is* access to all the variables in scope).

  The C++0x (well, C++1x, really) standard will most probably introduce
lambda functions and closures, which addresses all these problems, and
more. You will be able to write your function directly as a parameter
to for_each() (or whichever generic function you want to use) as a
lamdba function, and this lambda function can (optionally) have access
to all the variables in scope.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.