(C++) Negater
January 11, 2018 · View on GitHub
(C++) Negater
A negater is a type of functor that allows to express the opposite of a predicate [1].
Negaters are useful when using algorithms.
Example (using Boost.Bind)
The example below demonstrates how to a find a not-male (that is, a female) using the std::not1 negater.
#include <algorithm> #include <vector> #include <boost/bind.hpp> struct Person { Person(const bool is_male) : m_is_male(is_male) {} bool IsMale() const { return m_is_male; } const bool m_is_male; }; bool HasFemale(const std::vector<const Person *>& v) { return std::find_if( v.begin(),v.end(), std::not1(boost::mem_fn(&Person::IsMale)) ) != v.end(); }
References
- Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4. Chapter 18.4.4: 'A negater allows us to express the opposite of a predicate'