How to use Delphi's anonymous methods and generics in your C++Builder XE8 applications

Posted by on in Tips, Tricks and Techniques

With the release of C++Builder XE8 and RAD Studio XE8, we have made it even easier for C++ developers to work wth Delphi's anonymous methods and generics. We have updated the recommended way to handle Delphi anonymous methods in your C++ applications. The revised description, in How to Handle Delphi Anonymous Methods in C++, uses either a Functor (function object) or a Lambda expression. See the code examples on the following RAD Studio XE8 DocWiki pages.

You can also use a lambda wherever an API expects an anonymous method. The DelphiInterface class has been updated to auto-convert to a lambda. This feature can only be used with the Clang-based C++ Compilers. The following example C++ code illustrates use of a lambda.

#include <System.hpp>
#include <System.IOUtils.hpp>
#include <iostream>
 
int main()
{
  String ext(".cpp");                   
  TStringDynArray files = TDirectory::GetFiles(TDirectory::GetCurrentDirectory(),
                          [ext](const String Path, const System::Sysutils::TSearchRec &SearchRec) -> bool
                          {
                            return ExtractFileExt(SearchRec.Name) == ext;
                          });
  std::cout << "Found " << files.Length 
            << " files with ext: '" << AnsiString(ext).c_str() << "'\n";
  for (int i=0; i<files.Length; ++i)
    std::cout << AnsiString(files[i]).c_str() << std::endl;
}

 

Tags: C++ C++11 Lambda


About
Gold User, Rank: 1, Points: 2466
David Intersimone (known to many as David I.) is a passionate and innovative software industry veteran-often referred to as a developer icon-who extols and educates the world on Embarcadero developer tools. He shares his visions as an active member of the industry speaking circuit and is tapped as an expert source by the media. He is a long-standing champion of architects, developers and database professionals and works to ensure that their needs are folded into Embarcadero's strategic product plans. David holds a bachelor's degree in computer science from California Polytechnic State University at San Luis Obispo, California.

Comments

Check out more tips and tricks in this development video: