expression of a function object

E

ES Kim

Here's a code fragment:

struct S
{
operator size_t();
};

vector<S> v;
find_if(v.begin(), v.end(), bind2nd(less<size_t>(), 10));

It's something like finding an element in v which is less than 10.
You know conversion operators are often troublesome, so I decided to
change it to size_t S::size() const. Then how can I express the predicate
by using only the function objects in the standard?
I know I can do that by S::S(size_t), bool S::eek:perator<(const S&) const,
and bind2nd(less<S>(), 10). But let's leave out that possibility for now.
 
K

Kurt Krueckeberg

struct S
{
operator size_t();
};

vector<S> v;
find_if(v.begin(), v.end(), bind2nd(less<size_t>(), 10));

It's something like finding an element in v which is less than 10.
You know conversion operators are often troublesome, so I decided to
change it to size_t S::size() const. Then how can I express the predicate
by using only the function objects in the standard?
I know I can do that by S::S(size_t), bool S::eek:perator<(const S&) const,
and bind2nd(less<S>(), 10). But let's leave out that possibility for now.

"... how can I express the predicate by using only the function objects in
the standard?"

I think you may have answered your own question. bind2nd(less<S>(), 10) only
uses function objects that are part of the standard library.
 
T

Tom Widmer

Here's a code fragment:

struct S
{
operator size_t();
};

vector<S> v;
find_if(v.begin(), v.end(), bind2nd(less<size_t>(), 10));

It's something like finding an element in v which is less than 10.
You know conversion operators are often troublesome, so I decided to
change it to size_t S::size() const. Then how can I express the predicate
by using only the function objects in the standard?

You can't, since there is no "compose" functor or similar in the
current standard (it was an SGI STL extension AFAIK). Of course,
std::tr1::bind will do it.

Tom
 
E

ES Kim

Tom Widmer said:
You can't, since there is no "compose" functor or similar in the
current standard (it was an SGI STL extension AFAIK). Of course,
std::tr1::bind will do it.

Thank you for your hint. I found a solution using boost::compose,
which is deprecated in current release.

find_if(v.begin(), v.end(),
boost::compose_f_gx(bind2nd(less<size_t>(), 10),
mem_fun_ref(&S::size)));

BTW, is std::tr1::bind from the standard draft? I tried to download it
but I can't. Something might be wrong.
 
T

Tom Widmer

Thank you for your hint. I found a solution using boost::compose,
which is deprecated in current release.

find_if(v.begin(), v.end(),
boost::compose_f_gx(bind2nd(less<size_t>(), 10),
mem_fun_ref(&S::size)));

BTW, is std::tr1::bind from the standard draft? I tried to download it
but I can't. Something might be wrong.

This link just worked fine for me:
http://www2.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1711.pdf

bind is described in 3.6.3. Apparently the main site is suffering from
bandwidth restrictions.

Tom
 
J

Jeff Flinn

ES Kim said:
Thank you for your hint. I found a solution using boost::compose,
which is deprecated in current release.

find_if(v.begin(), v.end(),
boost::compose_f_gx(bind2nd(less<size_t>(), 10),
mem_fun_ref(&S::size)));

Which can be done with boost::bind:

using boost::bind;

bind( std::less<size_t>(), 10, bind( &S::size, _1 ) )


Jeff F
 
E

ES Kim

Jeff Flinn said:
Which can be done with boost::bind:

using boost::bind;

bind( std::less<size_t>(), 10, bind( &S::size, _1 ) )

It looks simpler. OK, I'll try that. Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,530
Latest member
jameswilliam1

Latest Threads

Top