replacement for std::binary_function
First, my advice is to watch CppCon 2015: Stephan T. Lavavej "functional: What's New, And Proper Usage". std::binary_function
is mentioned on slide 36, at around 36 mins in the video. You can find the slides at github.com/CppCon/CppCon2015). It doesn't go into detail why you shouldn't use std::binary_function
, but if you're using something that's been deprecated since C++11, then you would probably benefit from watching it.
If you want the actual rationale for not using it, try n4190:
unary_function/binary_function were useful helpers when C++98-era adaptors needed argument_type/etc. typedefs. Such typedefs are unnecessary given C++11's perfect forwarding, decltype, and so forth. (And they're inapplicable to overloaded/templated function call operators.) Even if a class wants to provide these typedefs for backwards compatibility, it can do so directly (at a minor cost in verbosity) instead of inheriting from unary_function/binary_function, which is what the Standard itself started doing when these helpers were deprecated.
Now you simply don't need it, so you can remove all traces of it from your program.
In C++14, transparent comparators were added. But it can be implemented in C++11. Just specialize it for void
:
template<>
struct absoluteLess<void> {
template< class T, class U>
constexpr auto operator()( T&& lhs, U&& rhs ) const
-> decltype(absolute(std::forward<T>(lhs)) < absolute(std::forward<U>(rhs)))
{
return absolute(std::forward<T>(lhs)) < absolute(std::forward<U>(rhs));
}
}
};
Now the type can be deduced:
std::max_element(v.begin(), v.end(), absoluteLess<>());
The only thing std::binary_function
does is providing the member typedefs result_type
, first_argument_type
, and second_argument_type
. And the only thing in the standard library that uses these typedefs is std::not2
, which is 1) strictly superseded by C++17 std::not_fn
, 2) easily replaced by a lambda anyway, and 3) deprecated in C++17 and likely going to be removed in the next revision.
If, for whatever reason, you need to use not2
, the legacy binders (bind1st
/bind2nd
, both deprecated in C++11 and removed in C++17), or some ancient third-party thing following that protocol, the replacement is to define the typedefs directly in your class:
using result_type = bool;
using first_argument_type = T;
using second_argument_type = T;
Otherwise, simply remove the inheritance.