|
for-in loop |
Top Previous Next |
|
What is translated > Statements > for loop's > for-in loop A Delphi 'for-in' loop has the following general form:
var A: TypeName; begin for A in B do DoSomething(A); end;
Depending on the type of 'B', the variable 'A' can represent:
Strings, arrays, and sets are usually translated into C++11 range-based 'for' loops:
TypeName A;
for (TypeName element_0 : B) { A = element_0; DoSomething(A); }
The additional variable 'element_0' allows Delphi2Cpp to preserve the declaration and assignment semantics of the original Delphi loop variable.
Open Arrays
For C++Builder open-array parameters, the array is passed as a pointer together with its highest valid index. A cast may therefore be required before the value can be used in a range-based 'for' loop:
template<typename T> void __fastcall ArrayOfConstLoop(const T* B, int B_maxidx) { T A;
for (auto element_0 : *(T (*)[B_maxidx + 1])B) { A = element_0; DoSomething(A); } }
'B_maxidx' is the highest valid index, not the number of elements. The corresponding array size is therefore 'B_maxidx + 1'.
The iterator support required for sets and open arrays is defined in 'd2c_systypes.h'.
Enumerator Types
A container can support Delphi 'for-in' loops by providing a 'GetEnumerator()' method. The enumerator is obtained once and then used for the entire loop:
auto enumerator_0 = B->GetEnumerator();
while (enumerator_0->MoveNext()) { TypeName A = enumerator_0->Current; DoSomething(A); }
Calling 'GetEnumerator()' only once is important because each call may create or return a different enumerator instance.
|
|
This page belongs to the Delphi2Cpp Documentation |
Delphi2Cpp home Content |