From C to C++: The Evolution of Function Pointers and Why It Matters

Kirubakaran
2 min readJun 21, 2024

If there is one feature which was ahead of time when it was released “function pointers” in C lang. I don’t need to illustrate with examples/use cases where its used. You can see all over the files in C code.

However, as like anything, it got evolved and C++ took the better part of function pointers into some standard library calls.

  • std::function: This class template in the C++ Standard Template Library (STL) provides a type-safe wrapper around function pointers. It can store any callable entity (functions, lambdas, function objects) that matches a specified signature, allowing for more flexible and generic programming.
  • Lambdas: Introduced in C++11, lambda expressions provide a concise way to define anonymous functions inline. They can capture variables from the surrounding scope, making them ideal for use with higher-order functions and algorithms that take functions as arguments.
  • STL Algorithms: The STL provides a rich set of algorithms that operate on ranges of elements. Many of these algorithms, like std::sort and std::for_each, accept functions as arguments, making them highly customizable. With lambdas and std::function, you can easily pass custom comparison or transformation functions to these algorithms.

--

--

Kirubakaran

Software Engineer expertise on C, C++, Golang, Python, Docker, Kubernetes.