Discussion:
functors and binary_function
(too old to reply)
Stephen Howe
2010-01-15 16:27:29 UTC
Permalink
Hi

Just reading "C++ Common Knowledge" - Stephen Dewhurst.

According to this, deriving your functor from binary_function allows "other parts of the STL implementation to ask compile-time
questions of the function object".

Now does this occur for
Visual Studio 8.0 ?
Visual Studio 9.0 ?
Visual Studio 10.0 ?

Thanks

Stephen Howe
Igor Tandetnik
2010-01-15 18:32:58 UTC
Permalink
Post by Stephen Howe
Just reading "C++ Common Knowledge" - Stephen Dewhurst.
According to this, deriving your functor from binary_function allows
"other parts of the STL implementation to ask compile-time questions
of the function object".
Now does this occur for
Visual Studio 8.0 ?
Visual Studio 9.0 ?
Visual Studio 10.0 ?
Yes, STL implementation included with all these VS versions provides std::binary_function class that you can derive from, as well as certain wrappers and adapters that benefit from typedefs std::binary_function introduces.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Stephen Howe
2010-01-16 21:21:25 UTC
Permalink
Post by Stephen Howe
Now does this occur for
Visual Studio 8.0 ?
Visual Studio 9.0 ?
Visual Studio 10.0 ?
Yes, STL implementation included with all these VS versions provides std::binary_function class that you can derive from...
I was certain of that Igor
... as well as certain wrappers and adapters that benefit from typedefs std::binary_function introduces.
What wrappers, adapters and benefits are those?

Thanks

Stephen Howe
Igor Tandetnik
2010-01-16 22:08:23 UTC
Permalink
Post by Stephen Howe
Post by Stephen Howe
Now does this occur for
Visual Studio 8.0 ?
Visual Studio 9.0 ?
Visual Studio 10.0 ?
Yes, STL implementation included with all these VS versions provides std::binary_function class that you can derive from...
I was certain of that Igor
... as well as certain wrappers and adapters that benefit from typedefs std::binary_function introduces.
What wrappers, adapters and benefits are those?
E.g. binary_negate, which is defined something like this:

template <class Predicate>
class binary_negate : public binary_function<
typename Predicate::first_argument_type,
typename Predicate::second_argument_type,
bool> {
bool operator() (
const first_argument_type& arg1,
const second_argument_type& arg2) const {

return !Predicate()(arg1, arg2);
}
};

Note how the template parameter (a user-defined predicate) is expected to provide typedefs named first_argument_type and second_argument_type. You can define them manually, or you can save some typing by deriving from binary_function.

There are a few other adapters in the similar vein.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Tamas Demjen
2010-01-18 21:32:24 UTC
Permalink
Post by Stephen Howe
... as well as certain wrappers and adapters that benefit from typedefs std::binary_function introduces.
What wrappers, adapters and benefits are those?
In addition to Igor's example, here's another adapter that I find
useful, to automatically resolve pointers:

template <class Predicate>
struct binary_resolve_ptr : public std::binary_function
<
typename const Predicate::first_argument_type*,
typename const Predicate::second_argument_type*,
bool
{
bool operator()(
const first_argument_type& arg1,
const second_argument_type& arg2) const
{
return Predicate()(*arg1, *arg2);
}
};

This comes handy for searching or sorting containers of pointers, but
it really shines in generic programming.

Now let's suppose that you have a predicate not inherited from
binary_function:

template <class T>
struct incorrect_less
{
bool operator()(const T& x, const T& y) const { return x < y; }
};

You wouldn't be able to use binary_resolve_ptr<incorrect_less<T>>.
Nevertheless, you could use incorrect_less for years without ever
noticing any problems with it in your own code.

Tom

Continue reading on narkive:
Loading...