A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../memory/new/../../language/replacement_function.html below:

Replacement functions - cppreference.com

Certain functions for which a definition is supplied by the implementation are replaceable . A C++ program may provide a definition with the signature of a replaceable function, called a replacement function . The replacement function, if provided, is used instead of the default version supplied by the implementation. Such replacement occurs prior to program startup.

If a declaration of the replacement function does not satisfy any of the following conditions, the program is ill-formed, no diagnostic is required:

Core language

It is implementation-defined whether the contract-violation handler ::handle_contract_violation is replaceable.

(since C++26) [edit] Standard library

The following standard library functions are replaceable, and the description of function semantics apply to both the default version defined by the C++ standard library and the replacement function defined by the program:

[edit] Example

Uses a replacement allocation function:

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

Output:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4