Home » Developing U++ » UppHub » Anonymous delegates
Re: Anonymous delegates [message #13030 is a reply to message #13019] |
Wed, 05 December 2007 21:33   |
Zardos
Messages: 62 Registered: April 2007
|
Member |
|
|
luzr wrote on Wed, 05 December 2007 15:33 |
Zardos wrote on Wed, 07 November 2007 21:17 | If you use it, please be aware it's still only a macro.
To illustrate the fundamentel problem a simple example:
Vector<int> CreateResultVector() {
Vector<int> r;
r.Add(1);
r.Add(2);
r.Add(3);
return r;
}
...
foreach(int e, CreateResultVector())
DUMP(e);
Basically the code is stupidly translated to something like this:
for(int i = 0; i < CreateResultVector().GetCount(); i++)
DUMP(CreateResultVector()[i]);
... CreateResultVector is called multiple times!
But this is probably not what you would expect from a real foreach build into the language!
|
Curiously, this is exactly what I would expect... (I mean, called multiple times).
Mirek
|
Yes, but you are a very experienced c++ programmer. You know "foreach" is a macro and simply expect "macro behaviour".
If a "foreach" would be available in c++ it would probably evaluate CreateResultVector only once like in C#, Python or Ruby.
For example the following ruby code evaluates create_result_vector only once:
def create_result_vector
[1, 2, 3]
end
for e in create_result_vector do
puts e
end
- Ralf
|
|
|
Goto Forum:
Current Time: Fri Jul 18 10:49:44 CEST 2025
Total time taken to generate the page: 0.03977 seconds
|