t2t-soft

Anonymous methods in Delphi are declared using the keyword reference to.
They represent closures: callable values that may capture variables from their surrounding scope.

In Delphi2Cpp, anonymous methods are translated to callable wrapper types.

  • Portable mode -> d2c::callback
  • C++Builder mode -> SysUtils callable interfaces (TProc, TFunc__N, etc.)

This preserves stronger type identity and better compatibility with Delphi semantics.


Delphi
type
  TFuncOfInt = reference to function(x: Integer): Integer;

C++ portable
typedef d2c::callback TFuncOfInt;

C++Builder
typedef System::Sysutils::_di_TFunc__2 System::Sysutils::_di_TFunc__2

The generated type can store
- lambdas
- free functions
- wrapped instance methods

Assigning an Anonymous Method

Delphi
var
  adder: TFuncOfInt;
begin
  adder :=
    function(X: Integer): Integer
    begin
      Result := X + Y;
    end;
end;

C++ portable
TFuncOfInt adder;
adder = [&](int X)
{
  return X + Y;
};

C++Builder
TFuncOfInt adder;
adder = d2c::make_sysutils_callable, int(int)>(
  [&](int X)
  {
    return X + Y;
  });





Anonymous Methods (reference to) - Comparison Table

Topic  Delphi  C++ Portable  C++Builder 
**Type declaration**  `TProc = reference to procedure;`  `using TProc = d2c::callback<void()>;`  `using TProc = System::Sysutils::_di_TProc;` 
**Representation**  Closure with captured state  std::function wrapper + identity  Interface with Invoke() 
**Assign lambda**  `p := procedure begin end;`  `p = [&](){ };`  `p = d2c::make_sysutils_callable<TProc>([]{});` 
**Assign free function**  `p := GlobalProc;`  `p = &GlobalProc;`  `p = d2c::bind_free_proc0(&GlobalProc);` 
**Assign method**  `p := Obj.Method;`  `p = d2c::make_callback(Obj,&C::Method);`  `p = d2c::bind_proc0(Obj,&C::Method);` 
**Invoke**  `p();`  `p();`  `p->Invoke();` 
**Assigned check**  `Assigned(p)`  `if (p)`  `if (p != nullptr)` 
**Pass as parameter**  by value  by value  interface reference 
**Return from function**  closure returned  lambda returned  interface returned 
**Use as event field**  property field  `d2c::callback`  `_di_TProc` 
**Capture variables**  automatic boxing  reference capture (no boxing)  boxed via interface instance 
**Type identity**  distinct types  preserved via typedef  preserved via distinct interfaces 
**Overload resolution**  exact type match  exact typedef match  exact interface type 
**Shared captured state**  supported  not automatic  supported 
**Lifetime extension**  automatic  not automatic  automatic via refcount 
**Equality**  reference equality  identity token  interface equality 













   deutsch Deutsch


 
Latest News
05/18/26
Delphi2Cpp 2.7: Translation heuristics [more...]

11/18/25
Delphi2Cpp 2.6: Delphi Interfaces [more...]



"We have successfully completed the projects, and the applications are (for the most part) already running at our customers' sites."





"Thanks for your great work, really appreciate the work you have done on zlib and compiling ... test case."


Mattewada, Udayabhaskar
Nokia India 02/01/2021




[from case study...]

"A masterpiece -- Delphi2Cpp has exceeded all my expectations by far."


Tony Hürlimann
virtual-optima 08/20/2011



"First off, I have to say WOW! Delphi2Cpp is doing a *fantastic* job!"


Daniel Flower
linkrealms 01/15/2011



"Though we have not finished the conversion yet, I'm glad that we've found you and could transform Eurocap to C++ with the help of Delphi2CB and you. (And I'm also glad that we could help you to make Delphi2CB better😉)"


Gáspár Huba


 
This website is generated from plain text with [Minimal Website]

Minimal Website
 
Minimal Website is made with TextTransformer

TextTransformer
 
TextTransformer is made with Borland C++Builder

  Borland