Reputation: 502
std::binary_function
is deprecated now and is going to be deleted in c++17. I searched on different publications, but I couldn't find a exact way to replace it. I'd like to know how should I write the following code in c++11 style.
template <class T>
inline T absolute(const T &x) {
return (x >= 0) ? x : -x;
}
template <class T>
struct absoluteLess : public std::binary_function<T, T, bool> {
bool operator()(const T &x, const T &y) const {
return absolute(x) < absolute(y);
}
};
template <class T>
struct absoluteGreater : public std::binary_function<T, T, bool> {
bool operator()(T &x, T &y) const {
return absolute(x) > absolute(y);
}
};
I'm using the functions in the following way:
output[j] = *std::max_element(input.begin() + prev,
input.begin() + pos,
absoluteLess<float>());
input
and output
are std::vector
s, inside a for-loop.
Upvotes: 37
Views: 21907
Reputation: 1718
I was able to solve a similar issue via std::function (as suggested by ulatekh). Applied to your code, it would look like this:
template <class T>
inline T absolute(const T &x) {
return (x >= 0) ? x : -x;
}
template <class T>
struct absoluteLess : public std::function<bool(T, T)> {
bool operator()(const T &x, const T &y) const {
return absolute(x) < absolute(y);
}
};
template <class T>
struct absoluteGreater : public std::function<bool(T, T)> {
bool operator()(T &x, T &y) const {
return absolute(x) > absolute(y);
}
};
In other words:
Just replace
std::binary_function<X, Y, Z>
by
std::function<Z(X, Y)>
Where X, Y and Z are your types (Z is the result type).
Upvotes: 2
Reputation: 14865
binary_function
can easily be replaced by lambda
functions:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Lambdas can be declared in the global scope
auto absolute = [](auto& x)->float{ return x<0?-x:x;};
int main()
{
// Lambdas can be declared embedded in functions
auto absoluteLess = [&](auto&x, auto&y)->bool{ return absolute(x)>absolute(y);};
auto absoluteGreater = [&](auto&x, auto&y)->bool{ return absolute(x)<absolute(y);};
std::vector<float> input={-2.0, 0.0, 3.4, -123.0};
std::cout << *std::max_element(input.begin(), input.end(), absoluteLess) <<std::endl;
std::cout << *std::max_element(input.begin(), input.end(), absoluteGreater) <<std::endl;
return 0;
}
Upvotes: 3
Reputation: 137425
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.
Upvotes: 23
Reputation: 256
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<>());
Upvotes: 24