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 |
実装によって定義が提供される特定の関数は replaceable(置換可能) です。C++プログラムは、置換可能関数の signature(シグネチャ) を持つ定義を提供することができ、これを replacement function(置換関数) と呼びます。提供された置換関数は、実装によって提供されるデフォルトバージョンの代わりに使用されます。このような置換はプログラムの開始前に行われます。
置換関数の宣言が以下のいずれかの条件を満たさない場合、プログラムは不適格であり、診断は不要です:
- これは inline ではありません。
- これはグローバルモジュールに attached されています。
- これはC++の language linkage を持ちます。
- これは置換可能関数と同じ戻り値の型を持ちます。
- 置換可能関数が 標準ライブラリヘッダー で宣言されている場合、そのヘッダー内の宣言の再宣言として有効です。
コア言語契約違反ハンドラ :: handle_contract_violation が置換可能かどうかは実装定義である。 |
(C++26以降) |
標準ライブラリ
以下の標準ライブラリ関数は置換可能であり、関数の意味論に関する記述は、C++標準ライブラリで定義されたデフォルトバージョンと、プログラムで定義された置換関数の両方に適用されます:
|
メモリ確保関数
(関数) |
|
|
メモリ解放関数
(関数) |
|
|
(C++26)
|
プログラムがデバッガの制御下で実行されているかどうかをチェックする
(関数) |
例
置換アロケーション関数を使用します:
#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 }
出力:
Replaced!