Functional Programming in C++: Modern Features and Examples
Functional programming (FP) is a paradigm in which computations are treated as the evaluation of mathematical functions, avoiding changing state and mutable data. Although C++ is traditionally associated with object-oriented and procedural programming, starting from C++11 and especially in C++14/17/20, the language has gained powerful tools for the functional style. In this article, we will break down key concepts that will make your C++ code more concise, safe, and expressive.
1. Lambda Expressions and Closures
Lambda expressions are anonymous functions that can be used inline. They became the foundation of functional programming in C++. A lambda captures variables from its environment (closure) and can be passed to STL algorithms.
Lambda Expression Syntax
auto lambda = [capture](parameters) -> return_type { body };Example: Sorting with a Lambda
#include <iostream>#include <vector>#include <algorithm>
int main() { std::vector<int> nums = {5, 2, 8, 1, 9}; // Sort in descending order std::sort(nums.begin(), nums.end(), [](int a, int b) { return a > b; }); for (int n : nums) std::cout << n << " "; // 9 8 5 2 1 return 0;}Closures: Capture by Value and Reference
#include <iostream>#include <vector>#include <algorithm>
int main() { int threshold = 5; std::vector<int> data = {1, 6, 3, 8, 2}; // Capture by value [=] - copy of threshold auto count_greater = std::count_if(data.begin(), data.end(), [=](int x) { return x > threshold; }); // Capture by reference [&] - can modify auto modify = [&](int &x) { if (x < threshold) x += threshold; }; std::for_each(data.begin(), data.end(), modify); std::cout << "Count: " << count_greater << ""; // Count: 2 for (int n : data) std::cout << n << " "; // 6 6 8 8 7 return 0;}2. Functors and std::function
Functors are class objects that overload the operator() operator. They can store state and behave like functions. std::function is a polymorphic wrapper for storing any callable objects.
Functor Example
#include <iostream>
class Adder {private: int base;public: Adder(int b) : base(b) {} int operator()(int x) const { return base + x; }};
int main() { Adder add5(5); std::cout << add5(10) << ""; // 15 std::cout << add5(20) << ""; // 25 return 0;}std::function: Universal Wrapper
#include <iostream>#include <functional>#include <vector>
void process(const std::vector<int>& data, std::function<bool(int)> predicate) { for (int x : data) { if (predicate(x)) std::cout << x << " "; } std::cout << "";}
int main() { std::vector<int> numbers = {1, 2, 3, 4, 5, 6}; // Can pass lambdas process(numbers, [](int n) { return n % 2 == 0; }); // 2 4 6 process(numbers, [](int n) { return n > 3; }); // 4 5 6 // And functors struct GreaterThan { int limit; bool operator()(int n) const { return n > limit; } }; process(numbers, GreaterThan{4}); // 5 6 return 0;}3. STL Algorithms in Functional Style
The STL library provides many algorithms that pair perfectly with lambdas and functors. Instead of loops, use std::transf