std::not1(3)
π NAME
std::not1 β bestie, it's literally the vibe-flipper for your unary predicates π
(and it's from the C++ Standard Library, so like⦠vintage)
β¨ SYNOPSIS
Okay so first things first, it lives in the header <functional> π
Here's the tea, era by era:
until C++14:
template< class Predicate >
std::unary_negate<Predicate> not1( const Predicate& pred );
since C++14:
template< class Predicate >
constexpr std::unary_negate<Predicate> not1( const Predicate& pred );
π«Ά heads up:constexprgot added in C++14, and thenstd::not1was deprecated in C++17, and likeβ¦ fully removed in C++20 π so if you're on a modern standard, it's giving "gone, reduced to atoms."
So std::not1 is a helper function β a little matchmaker that creates a function object returning the complement of the unary predicate function you passed in π₯ The function object it creates is of type std::unary_negate<Predicate>. No cap.
Now, there's a requirement (iykyk) π β your unary predicate type must define a member type, argument_type, that is convertible to the predicate's parameter type. Don't ask, it was the 2000s.
And it does come pre-defined for a whole squad of unary function objects: the ones you obtain from std::ref, std::cref, std::negate, std::logical_not, std::mem_fn, std::function, std::hash, or from another call to std::not1 β all of these have that type defined. Same goes for function objects derived from the deprecated std::unary_function πΈ
π¦ PARAMETERS
predβ your unary predicate, bestie β¨
π RETURN VALUE
std::not1 returns an object of type std::unary_negate<Predicate>, constructed with pred. Simple as that, king behavior π
π EXCEPTIONS
(none) β literally zero drama, we love a low-maintenance icon π
π EXAMPLE
// Run this code
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
struct LessThan7 : std::unary_function<int, bool>
{
bool operator()(int i) const { return i < 7; }
};
int main()
{
std::vector<int> v(10);
std::iota(std::begin(v), std::end(v), 0);
std::cout << std::count_if(begin(v), end(v), std::not1(LessThan7())) << '\n';
// the same as above using std::function
std::function<bool(int)> less_than_9 = [](int x) { return x < 9; };
std::cout << std::count_if(begin(v), end(v), std::not1(less_than_9)) << '\n';
}
β Output
3
1
π SEE ALSO
not_fn(C++17) β creates a function object that returns the complement of the result of the function object it holds (function template) π¦ ngl this is the glow-up replacement, look at herunary_negate(deprecated in C++17) (removed in C++20) β wrapper function object returning the complement of the unary predicate it holds (class template) π₯² the original divafunction(C++11) β wraps callable object of any copy constructible type with specified function call signature (class template) β¨move_only_function(C++23) β wraps callable object of any type with specified function call signature (class template) π₯ new gen, main character energynot2(deprecated in C++17) (removed in C++20) β constructs customstd::binary_negateobject (function template) π the two-arg sibling, also goneptr_fun(deprecated in C++11) (removed in C++17) β creates an adaptor-compatible function object wrapper from a pointer to function (function template) π» ancient artifactunary_function(deprecated in C++11) (removed in C++17) β adaptor-compatible unary function base class (class template) π«Ά the mother ofargument_typeherself
okay bestie that's genuinely the whole page π stay safe out there and pls don't use std::not1 in new code, it's deprecated-then-removed and I want better for you π use not_fn fr fr.
sourced from http://cppreference.com, dated 2024.06.10 πΈ mwah, go slay some predicates π§ββοΈβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms