Generic interfaces

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Interfaces > Generic interfaces

Generic interfaces in Delphi (IComparer<T>, IEqualityComparer<T>, etc.) extend the flexibility of the language by allowing interfaces to be parameterized with types.

Unlike non-generic interfaces, they are not based on COM and therefore do not implement reference counting or GUID-based type resolution. Instead, they are typically used as type contracts for algorithms and collections within the Delphi RTL.

 

Generic interfaces are always compile-time constructs in Delphi. They are normally instantiated as static, global singletons (e.g., TComparer<T>.Default), which are reused throughout the program. This avoids the need for explicit memory management or IUnknown-style reference counting.

 

Scope of translation

 

In Delphi2Cpp, generic interfaces are translated into plain C++ abstract classes (i.e., struct with pure virtual methods).

They do not carry GUIDs and do not participate in QueryInterface, Supports, or as<T> resolution.

 

Instead, they are used directly in template-based algorithms, with their lifetime managed like any ordinary C++ object.

For example:

 

IComparer<T> → used to implement custom comparison logic in TArray::BinarySearch or TDictionary

 

IEqualityComparer<T> → used to define key equality in TDictionary or THashSet

 

Because no reference counting is involved, these interfaces can safely be created as stack objects, singletons, or wrapped in standard smart pointers (std::unique_ptr, std::shared_ptr) if needed.



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content