Logo
Articles Compilers Libraries Books MiniBooklets Assembly C++ Rust Go Linux CPU Others Videos
Advertisement

Article by Ayman Alheraki on April 4 2026 04:09 PM

Everything You Need to Know About #include functional in C++

Everything You Need to Know About Functional header in C++

The <functional> header is one of the most powerful and versatile parts of the C++ Standard Library. It provides tools for working with callable objects – things you can invoke like functions. Whether you're using lambdas, function pointers, functors, or member functions, <functional> helps you store, bind, adapt, and pass them around seamlessly.

In this article, we’ll explore every major component of <functional> with clear, practical examples. By the end, you’ll know how to use std::function, std::bind, placeholders, std::reference_wrapper, std::mem_fn, and more.


1. What Is <functional>?

At its core, <functional> provides:

  • Polymorphic function wrappers (std::function) – a type‑erased container for any callable.

  • Function binders (std::bind, placeholders) – to fix arguments or reorder them.

  • Reference wrappers (std::reference_wrapper, std::ref, std::cref) – to store references in copy‑heavy contexts.

  • Member function adapters (std::mem_fn) – to turn member functions into ordinary function objects.

  • Function objects for arithmetic, comparisons, and logical operations (e.g., std::plus, std::greater, std::logical_and).

All of these are designed to work seamlessly with the <algorithm> library and modern C++ idioms.


2. std::function – The Universal Callable Wrapper

std::function is a type‑erased wrapper that can hold any callable object with a given signature. It’s like a super‑charged function pointer.

Basic Usage

cpp

 

Why Use std::function?

  • Type erasure – you can store different callables in the same container (e.g., std::vector<std::function<bool(int,int)>>).

  • Nullability – a std::function can be empty; test it with !func or func == nullptr.

  • Target access – use func.target<T>() to retrieve the stored object (advanced).

Performance Note

std::function has some overhead (virtual dispatch, possible small‑object optimisation). For hot paths, consider using templates or raw function pointers. But for callbacks, event systems, or configuration, it’s invaluable.

Example: Callbacks

cpp

 


3. std::bind – Fixing and Reordering Arguments

Before lambdas (C++11), std::bind was the main way to create adapters. Today, lambdas are often clearer, but std::bind still shines in generic code or when you need to bind member functions.

Syntax

cpp

 

Placeholders _1, _2, … (from std::placeholders) represent arguments that will be supplied when the bound object is called.

Basic Examples

cpp

 

When to Prefer std::bind Over Lambda?

  • Very generic codestd::bind works with any callable, including member pointers, without needing std::mem_fn.

  • Perfect forwarding of placeholders – lambdas require decltype tricks.

  • Compile‑time argument reordering – sometimes more concise than a lambda.

But lambdas are generally more readable. For new code, use lambdas unless you have a specific reason.


4. Placeholders – _1, _2, _3, …

Placeholders live in the std::placeholders namespace. They tell std::bind which arguments to forward and where.

cpp

 


5. std::ref and std::cref – Reference Wrappers

By default, std::bind and std::function copy their arguments. To store or pass a reference, use std::ref (non‑const) or std::cref (const). They create a std::reference_wrapper<T>.

Why Are They Needed?

cpp

 

Using std::ref with std::function

cpp

 

You can also store a reference wrapper directly:

cpp

 

When to Use std::cref

When you want to pass a const reference, e.g., to avoid copying a large object into a lambda or bound callable.

cpp

 


6. std::mem_fn – Wrapping Member Functions

std::mem_fn turns a pointer to a member function into a callable object that can be invoked with an object (or pointer) as the first argument.

Syntax

cpp

 

Example

cpp

 

Comparison with std::bind & Lambdas

  • std::mem_fn is cleaner than std::bind(&Person::greet, _1).

  • Lambdas are equally clear: [&](Person& p){ p.greet(); }.

  • Use std::mem_fn when you already have a callable object and want to pass it to an algorithm without extra braces.


7. Built‑in Function Objects – Arithmetic, Comparisons, Logic

The <functional> header provides templated functors for common operations. These are useful with std::transform, std::sort, etc., and often allow the compiler to inline better than a hand‑rolled lambda.

Arithmetic

FunctorOperation
std::plus<T>+
std::minus<T>-
std::multiplies<T>*
std::divides<T>/
std::modulus<T>%
std::negate<T>- (unary)

Comparisons

FunctorOperation
std::equal_to<T>==
std::not_equal_to<T>!=
std::greater<T>>
std::less<T><
std::greater_equal<T>>=
std::less_equal<T><=

Logical

FunctorOperation
std::logical_and<T>&&
std::logical_or<T>||
std::logical_not<T>!

Example

cpp

 


8. Advanced: std::function with Move‑Only Types

std::function requires the stored callable to be copy‑constructible. For move‑only callables (e.g., a lambda capturing a std::unique_ptr), you need a different approach – like std::move_only_function (C++23) or a custom wrapper. In C++14/17, you can use std::packaged_task or design your own type‑erased interface.

cpp

 


9. Performance & Best Practices

When to Use Each Tool

ToolUse case
Raw function pointerVery lightweight, no capture, fixed signature.
Lambda (auto)Most local uses, best optimisation.
std::functionStoring heterogeneous callables, callbacks, type erasure needed.
std::bindLegacy code or advanced perfect‑forwarding scenarios.
std::mem_fnWhen you need a callable for a member function, especially in generic algorithms.
std::ref / std::crefAny time you want to avoid copying into a bind/function.
Built‑in functorsFor standard operations, often faster than lambdas (compiler knows them).

Avoid Unnecessary std::function

cpp

 

Small‑Object Optimisation

std::function typically stores small callables (e.g., a lambda with no captures) inline, avoiding heap allocation. For larger objects, it allocates on the heap.


10. Complete Practical Example – Event System

Let’s tie everything together in a small event dispatcher.

cpp

 

Output:

text

 


Conclusion

#include <functional> is an essential tool for modern C++ developers. It provides:

  • std::function for type‑erased callables,

  • std::bind and placeholders for argument adaptation,

  • std::ref / std::cref for reference semantics,

  • std::mem_fn for member function pointers,

  • Convenient function objects for standard operations.

While lambdas have replaced many uses of std::bind, the <functional> header remains indispensable for writing flexible, generic, and expressive C++ code. Use it wisely, and your callbacks, algorithms, and event systems will become both powerful and clear.

Now go ahead – #include <functional> and unleash the full potential of callable objects in C++!

Advertisements

Responsive Counter
General Counter
1195543
Daily Counter
2594