The C++ std
namespace contains the helper functions std::not1
and std::not2
. They both take a unary or binary predicate functor, respectively, and return a std::unary_negate
or std::binary_negate
predicate, respectively.
I was wondering whether it should not be possible using some template magic to have
template<typename Predicate> inline
enable_if_t<is_unary_predicate<Predicate>::value, unary_negate<Predicate> >
not_(Predicate const&pred)
{ return unary_negate<Predicate>{pred}; }
template<typename Predicate> inline
enable_if_t<is_binary_predicate<Predicate>::value, binary_negate<Predicate> >
not_(Predicate const&pred)
{ return binary_negate<Predicate>{pred}; }
which distinguishes the argument pred
passed to return the appropriate predicate. Of course, there are the odd cases where the passed object pred
has both types of operators (unary and binary), when this will not work, but these can be dealt without the use of this helper function.
not
is a keyword, hence unusable – Dambrosionot_()
then. – TechnologyX_negate
instead ofX_predicate
? – TiltBeing able to use lambda is really an improvement here
– Styraxnot_
would be possible in C++11 thanks to variadic template, I'm not sure if the traits to identify unary predicate is possible. What I get miss some case: Demo – Suspensoid