Topic: Saving instances of boost::lambda functions for later use : possible ??


Author: Fuzzy Coder <nospam@me.net>
Date: Wed, 14 Jul 2010 11:32:37 CST
Raw View
Hello there.

I have been trying to do something to create generic function objects
in an ad-hoc fashion, using boost::lambda instead of defining reams of
function classes.

So conceptually I want to do something like :

typedef TD comp_type  ;

vector<TD> s ;
s.push_back(make_a_lambda( _1 < _2) ) ;
s.push_back(make_a_lambda( _1 <= _2) ) ;
// etc etc

res = binary_search(c_begin,c_end,item, s[0] ) ;


Is it possible to do, or do the means already exist in Boost, to
do the above (ie devise definitions for "TD" and "make_a_lambda" ) ??


I have tried, but the Boost library docs are just woeful for this kind
of stuff. I have tried doing things like :

typedef boost::function<bool, ( T const&, T const& ) > TD ;

But to no avail. :-(


Whether this can be done or not, anyone who could put me out my misery
on either front would be helping me a lot.


Best Regards

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Vaclav Haisman <v.haisman@sh.cvut.cz>
Date: Thu, 15 Jul 2010 14:08:20 CST
Raw View
Fuzzy Coder wrote, On 14.7.2010 19:32:
> Hello there.
>
> I have been trying to do something to create generic function objects
> in an ad-hoc fashion, using boost::lambda instead of defining reams of
> function classes.
>
> So conceptually I want to do something like :
>
> typedef TD comp_type  ;
>
> vector<TD> s ;
> s.push_back(make_a_lambda( _1 < _2) ) ;
> s.push_back(make_a_lambda( _1 <= _2) ) ;
> // etc etc
>
> res = binary_search(c_begin,c_end,item, s[0] ) ;
>
>
> Is it possible to do, or do the means already exist in Boost, to
> do the above (ie devise definitions for "TD" and "make_a_lambda" ) ??
>
>
> I have tried, but the Boost library docs are just woeful for this kind
> of stuff. I have tried doing things like :
>
> typedef boost::function<bool, ( T const&, T const& ) > TD ;
Drop the comma here ----------^
Untested.

>
> But to no avail. :-(
>
>
> Whether this can be done or not, anyone who could put me out my misery
> on either front would be helping me a lot.

--
VH

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Thu, 15 Jul 2010 14:08:48 CST
Raw View
On Jul 14, 7:32 pm, Fuzzy Coder <nos...@me.net> wrote:
> I have been trying to do something to create generic function objects
> in an ad-hoc fashion, using boost::lambda instead of defining reams of
> function classes.
>
> So conceptually I want to do something like :
>
> typedef TD comp_type  ;
>
> vector<TD> s ;
> s.push_back(make_a_lambda( _1 < _2) ) ;
> s.push_back(make_a_lambda( _1 <= _2) ) ;
> // etc etc
>
> res = binary_search(c_begin,c_end,item, s[0] ) ;
>
> Is it possible to do, or do the means already exist in Boost, to
> do the above (ie devise definitions for "TD" and "make_a_lambda" ) ??

boost::function is the appropriate tool for that (and it will be
std::function in C++0x).

> I have tried, but the Boost library docs are just woeful for this kind
> of stuff. I have tried doing things like :
>
> typedef boost::function<bool, ( T const&, T const& ) > TD ;
>
> But to no avail. :-(

Use

typedef boost::function<bool ( T const&, T const& )> TD ;

(note the missing ",")

or

typedef boost::function2<bool, T const&, T const&> TD ;

instead.

HTH & Greetings from Bremen,

Daniel Kr   gler


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Thu, 15 Jul 2010 14:34:37 CST
Raw View
On Jul 14, 6:32 pm, Fuzzy Coder <nos...@me.net> wrote:

> So conceptually I want to do something like :
>
> typedef TD comp_type  ;
>
> vector<TD> s ;
> s.push_back(make_a_lambda( _1 < _2) ) ;
> s.push_back(make_a_lambda( _1 <= _2) ) ;
> // etc etc
>
> res = binary_search(c_begin,c_end,item, s[0] ) ;
>
> Is it possible to do, or do the means already exist in Boost, to
> do the above (ie devise definitions for "TD" and "make_a_lambda" ) ??

Use boost::function or std::function from TR1.


> I have tried, but the Boost library docs are just woeful for this kind
> of stuff. I have tried doing things like :
>
> typedef boost::function<bool, ( T const&, T const& ) > TD ;

Remove the first comma and it should work, assuming T is actually a
type here.
I'm not that familiar with something as crusty and old as
boost::lambda, it might be that it not supporting the result_of
protocol causes problems.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]