std::not1(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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: constexpr got added in C++14, and then std::not1 was 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 her
  • unary_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 diva
  • function (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 energy
  • not2 (deprecated in C++17) (removed in C++20) β€” constructs custom std::binary_negate object (function template) πŸ’… the two-arg sibling, also gone
  • ptr_fun (deprecated in C++11) (removed in C++17) β€” creates an adaptor-compatible function object wrapper from a pointer to function (function template) 🎻 ancient artifact
  • unary_function (deprecated in C++11) (removed in C++17) β€” adaptor-compatible unary function base class (class template) 🫢 the mother of argument_type herself

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