Replacement functions
      From cppreference.net
     
     
     
        
         C++
        
        
         
          
           
          
          
         
        
       
       
        
         C++ language
        
        
         
          
           
            
             
           
          
          
           
            
             
           
          
          
          
         
        
       
       | 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 | ||||||||||||||||
        
         Functions
        
        
         
          
           
            
             
           
          
          
          
         
        
       
       | 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 | 
某些由实现提供定义的函数是 可替换的 。C++ 程序可以提供具有可替换函数签名的定义,称为 替换函数 。如果提供了替换函数,则将使用该函数而非实现提供的默认版本。此类替换发生在程序启动之前。
如果替换函数的声明不满足以下任一条件,则程序非良构,不要求诊断:
| 核心语言特性是否可替换 合约违反处理器 :: 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!