Replacement functions
| General topics | ||||||||||||||||
| Flow control | ||||||||||||||||
| Conditional execution statements | ||||||||||||||||
| Iteration statements (loops) | ||||||||||||||||
|
||||||||||||||||
| Jump statements | ||||||||||||||||
| Functions | ||||||||||||||||
| Function declaration | ||||||||||||||||
| Lambda function expression | ||||||||||||||||
inline
specifier
|
||||||||||||||||
| Dynamic exception specifications ( until C++17* ) | ||||||||||||||||
noexcept
specifier
(C++11)
|
||||||||||||||||
| Exceptions | ||||||||||||||||
| Namespaces | ||||||||||||||||
| Types | ||||||||||||||||
| Specifiers | ||||||||||||||||
|
||||||||||||||||
| Storage duration specifiers | ||||||||||||||||
| Initialization | ||||||||||||||||
| Expressions | ||||||||||||||||
| Alternative representations | ||||||||||||||||
| Literals | ||||||||||||||||
| Boolean - Integer - Floating-point | ||||||||||||||||
| Character - String - nullptr (C++11) | ||||||||||||||||
| User-defined (C++11) | ||||||||||||||||
| Utilities | ||||||||||||||||
| Attributes (C++11) | ||||||||||||||||
| Types | ||||||||||||||||
typedef
declaration
|
||||||||||||||||
| Type alias declaration (C++11) | ||||||||||||||||
| Casts | ||||||||||||||||
| Memory allocation | ||||||||||||||||
| Classes | ||||||||||||||||
| Class-specific function properties | ||||||||||||||||
|
||||||||||||||||
| Special member functions | ||||||||||||||||
|
||||||||||||||||
| Templates | ||||||||||||||||
| Miscellaneous | ||||||||||||||||
| Declarations | ||||
| Function declaration | ||||
| Function parameter list | ||||
| Function definition | ||||
| Function contract specifiers (C++26) | ||||
| Default arguments | ||||
| Variadic arguments | ||||
inline
specifier
|
||||
| Lambda expressions (C++11) | ||||
| Coroutines (C++20) | ||||
| Replacement functions | ||||
| Function calls | ||||
| Argument-Dependent Lookup (ADL) | ||||
| Function-call operator | ||||
| Function objects | ||||
| Overloading | ||||
| Overload resolution | ||||
| Operator overloading | ||||
| Address of an overload set |
Bestimmte Funktionen, für die eine Definition durch die Implementierung bereitgestellt wird, sind ersetzbar . Ein C++-Programm kann eine Definition mit der Signatur einer ersetzbaren Funktion bereitstellen, die als Ersatzfunktion bezeichnet wird. Die Ersatzfunktion, falls bereitgestellt, wird anstelle der standardmäßig durch die Implementierung gelieferten Version verwendet. Ein solcher Austausch erfolgt vor Programmstart.
Wenn eine Deklaration der Ersetzungsfunktion keine der folgenden Bedingungen erfüllt, ist das Programm fehlerhaft, keine Diagnose ist erforderlich:
- Es ist nicht inline .
- Es ist attached zum globalen Modul.
- Es besitzt C++ language linkage .
- Es hat denselben Rückgabetyp wie die ersetzbare Funktion.
- Wenn die ersetzbare Funktion in einem standard library header deklariert ist, wäre sie gültig als Neudeklaration der Deklaration in diesem Header.
KernspracheEs ist implementierungsdefiniert, ob der Vertragsverletzungs-Handler :: handle_contract_violation ersetzbar ist. |
(seit C++26) |
Standardbibliothek
Die folgenden Standardbibliotheksfunktionen sind ersetzbar, und die Beschreibung der Funktionssemantik gilt sowohl für die Standardversion, die durch die C++-Standardbibliothek definiert ist, als auch für die Ersatzfunktion, die durch das Programm definiert wird:
|
Allokierungsfunktionen
(Funktion) |
|
|
Freigabefunktionen
(Funktion) |
|
|
(C++26)
|
prüft, ob ein Programm unter der Kontrolle eines Debuggers läuft
(Funktion) |
Beispiel
Verwendet eine Ersatz-Allokationsfunktion:
#include <cstddef> #include <new> #include <print> // replacement function void* operator new(std::size_t count) { std::print("Replaced!"); return nullptr; } int main() { int* ptr = new int; // invokes the replacement version defined by the program }
Ausgabe:
Replaced!