# std::copyable_function

> C++ Atlas: `cppatlas.com/w/cpp/utility/functional/copyable_function` | Source: https://en.cppreference.com/w/cpp/utility/functional/copyable_function | Domain: Utility | Standards: C++11, C++23, C++26

Class template std::copyable_function is a general-purpose polymorphic function wrapper.

## Official Specification Summary (cppreference)

```cpp
// Defined in header <functional>

template< class... >
class copyable_function; // not defined // (1) (since C++26)

template< class R, class... Args >
class copyable_function<R(Args...)>;
template< class R, class... Args >
class copyable_function<R(Args...) noexcept>;
template< class R, class... Args >
class copyable_function<R(Args...) &>;
template< class R, class... Args >
class copyable_function<R(Args...) & noexcept>;
template< class R, class... Args >
class copyable_function<R(Args...) &&>;
template< class R, class... Args >
class copyable_function<R(Args...) && noexcept>;
template< class R, class... Args >
class copyable_function<R(Args...) const>;
template< class R, class... Args >
class copyable_function<R(Args...) const noexcept>;
template< class R, class... Args >
class copyable_function<R(Args...) const &>;
template< class R, class... Args >
class copyable_function<R(Args...) const & noexcept>;
template< class R, class... Args >
class copyable_function<R(Args...) const &&>;
template< class R, class... Args >
class copyable_function<R(Args...) const && noexcept>; // (2) (since C++26)
```

Class template `std::copyable_function` is a general-purpose polymorphic function wrapper. `std::copyable_function` objects can store and invoke any *CopyConstructible* *Callable* *target* — functions, lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to member objects.

The stored callable object is called the *target* of `std::copyable_function`. If a `std::copyable_function` contains no target, it is called *empty*. Unlike `std::function`, invoking an *empty* `std::copyable_function` results in undefined behavior.

`std::copyable_function`s supports every possible combination of cv-qualifiers (not including `volatile`), ref-qualifiers, and noexcept-specifiers provided in its template parameter. These qualifiers and specifier (if any) are added to its `operator()`.

`std::copyable_function` satisfies the requirements of *CopyConstructible* and *CopyAssignable*.

### Member types

**Type — Definition**

- `result_type`: `R`

### Member functions

- (constructor): constructs a new `std::copyable_function` object (public member function)
- (destructor): destroys a `std::copyable_function` object (public member function)
- `operator=`: replaces or destroys the target (public member function)
- `swap`: swaps the targets of two `std::copyable_function` objects (public member function)
- `operator bool`: checks if the `std::copyable_function` has a target (public member function)
- `operator()`: invokes the target (public member function)

### Non-member functions

- `swap(std::copyable_function)` (C++26): overloads the `std::swap` algorithm (function)
- `operator==` (C++26): compares a `std::copyable_function` with `nullptr` (function)

### Notes

Implementations may store a callable object of small size within the `std::copyable_function` object. Such small object optimization is effectively required for function pointers and `std::reference_wrapper` specializations, and can only be applied to types `T` for which `std::is_nothrow_move_constructible_v<T>` is `true`.

| Feature-test macro | Value | Std | Feature |
| --- | --- | --- | --- |
| `__cpp_lib_copyable_function` | `202306L` | (C++26) | `std::copyable_function` |

### Example

> Note: This section is incomplete Reason: no example

### See also

- [`function`](https://en.cppreference.com/w/cpp/utility/functional/function) (C++11): copyable wrapper of any copy constructible callable object (class template)
- [`move_only_function`](https://en.cppreference.com/w/cpp/utility/functional/move_only_function) (C++23): move-only wrapper of any callable object that supports qualifiers in a given call signature (class template)
- [`function_ref`](https://en.cppreference.com/w/cpp/utility/functional/function_ref) (C++26): non-owning wrapper of any callable object (class template)

---
Attribution: content from https://en.cppreference.com/w/cpp/utility/functional/copyable_function (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.