[c++] std::not_fn

When called, std::not_fn creates a new function object that, when invoked, returns the logical negation of the result of calling the original function. It is particularly useful in functional programming and when working with algorithms that require predicate functions.

Here’s an example of how std::not_fn can be used to create a negated function object:

#include <iostream>
#include <functional>

bool is_even(int x) {
    return x % 2 == 0;

int main() {
    auto is_odd = std::not_fn(is_even);

    std::cout << std::boolalpha;
    std::cout << "Is 5 odd? " << is_odd(5) << std::endl;
    std::cout << "Is 6 odd? " << is_odd(6) << std::endl;

    return 0;

In this example, is_even is a predicate function that checks if a number is even. We then use std::not_fn to create a function object is_odd that negates the result of is_even. When is_odd is invoked with a number, it returns the logical negation of the result obtained by calling is_even.

For more information, you can refer to the C++ reference for std::not_fn.