Namespaces
Variants

Replacement functions

From cppreference.net
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
constexpr (C++11)
consteval (C++20)
constinit (C++20)
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

구현체가 정의를 제공하는 특정 함수들은 대체 가능(replaceable)  합니다. C++ 프로그램은 대체 가능 함수의 시그니처(signature) 를 가진 정의를 제공할 수 있으며, 이를 대체 함수(replacement function)  라고 합니다. 제공된 대체 함수는 구현체가 제공하는 기본 버전 대신 사용됩니다. 이러한 대체는 프로그램 시작 전에 발생합니다.

대체 함수의 선언이 다음 조건 중 어느 하나라도 만족하지 않으면, 프로그램의 형식이 잘못되었으며 진단은 요구되지 않습니다:

  • 이것은 inline 이 아닙니다.
  • 이것은 전역 모듈에 attached 되어 있습니다.
  • 이것은 C++ language linkage 를 가집니다.
  • 이것은 대체 가능 함수와 동일한 반환 타입을 가집니다.
  • 대체 가능 함수가 표준 라이브러리 헤더 에 선언된 경우, 해당 헤더의 선언을 재선언하는 것으로 유효합니다.

핵심 언어

계약 위반 핸들러 :: handle_contract_violation 의 대체 가능 여부는 구현에 따라 정의됩니다.

(C++26부터)

표준 라이브러리

다음 표준 라이브러리 함수들은 대체 가능하며, 함수 의미론에 대한 설명은 C++ 표준 라이브러리에 의해 정의된 기본 버전과 프로그램에 의해 정의된 대체 함수 모두에 적용됩니다:

할당 함수
(함수)
해제 함수
(함수)
프로그램이 디버거 제어 하에 실행 중인지 확인
(함수)

예제

대체 할당 함수를 사용합니다:

#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!