On the richness of C++
Bruno Medeiros
brunodomedeiros+spam at com.gmail
Fri Apr 25 16:19:11 PDT 2008
Jason House wrote:
>
>
>>> Closures capture variables by reference. This means that creating
>>> delegates inside a foreach loop (with deferred evaluation) could fail
>>> to have the expected behavior. Bind stores stuff by value, so I
>>> still find myself using bind libraries.
>> If you expect captured variables to be by value, sure. But I always
>> expected them to be by reference!
>
> By reference is nice in many cases, but in others it's frustrating. How do
> you implement the following?
>
> foreach(job; queue)
> runthread(void delegate(){job.execute;});
>
> (Somehow I expect someone to pick apart the example, but I hope the point is
> clear)
This way:
foreach(job; queue) {
({
auto myjob = job;
runthread(void delegate(){myjob.execute;});
})();
}
And I'm pretty sure that it is only due to a bug that this doesn't work:
foreach(job; queue) {
auto myjob = job;
runthread(void delegate(){myjob.execute;});
}
(I'll file a report)
Like Walter said, it's a lot harder to make by-value closures into
by-reference than the other way around. You may bask in the glory of D
now. :)
--
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
More information about the Digitalmars-d
mailing list