|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
naming delayed variables string in boost lamdbaI am getting familiar with boost::lambda. Today I learnt about delayed variable and usage of boost::constant in lambda expression. Do you know why when I try to declare delayed variables for string like I do below the compiler gives me each time an error? I tried the following constant_type<const char*>::type _msg_a(constant("my msg")); constant_type<const std::string>::type _msg_b(constant("my msg")); constant_type<std::string>::type _msg_c(constant("my msg")); If I use constant( "my msg" ) inside a boost lambda expression everything is fine but I cannot declare correctly a delayed variable. Do you know how to fix it or where I can get some details? Thanks in advance Mn Chiacchiera con i tuoi amici da Messenger.it _______________________________________________ Boost-users mailing list Boost-users@... http://lists.boost.org/mailman/listinfo.cgi/boost-users |
|
|
Re: naming delayed variables string in boost lamdba2009/11/11 Emanuele Rocci <rocciemanuele@...>
Function objects generated by lambda have unspecified types. If you want to save them, use Boost.Function.
#include <iostream> #include <boost/lambda/core.hpp>
#include <boost/function.hpp>
int main() { boost::function<const char*()> f = boost::lambda::constant("hello");
std::cout << f() << std::endl; }
Roman Perepelitsa _______________________________________________ Boost-users mailing list Boost-users@... http://lists.boost.org/mailman/listinfo.cgi/boost-users |
|
|
Re: naming delayed variables string in boost lamdbaOn Wed, Nov 11, 2009 at 9:24 AM, Roman Perepelitsa
<roman.perepelitsa@...> wrote: > 2009/11/11 Emanuele Rocci <rocciemanuele@...> >> >> Hi All >> I am getting familiar with boost::lambda. >> Today I learnt about delayed variable and usage of boost::constant in >> lambda expression. >> Do you know why when I try to declare delayed variables for string like I >> do below >> the compiler gives me each time an error? >> I tried the following >> constant_type<const char*>::type _msg_a(constant("my msg")); >> constant_type<const std::string>::type _msg_b(constant("my msg")); >> constant_type<std::string>::type _msg_c(constant("my msg")); >> If I use constant( "my msg" ) inside a boost lambda expression everything >> is fine but I cannot declare correctly a delayed variable. >> Do you know how to fix it or where I can get some details? >> Thanks in advance >> Mn > > Function objects generated by lambda have unspecified types. If you want to > save them, use Boost.Function. > #include <iostream> > #include <boost/lambda/core.hpp> > #include <boost/function.hpp> > int main() { > boost::function<const char*()> f = boost::lambda::constant("hello"); > std::cout << f() << std::endl; > } Or BOOST_AUTO (which will have lower overhead then Boost.Function). _______________________________________________ Boost-users mailing list Boost-users@... http://lists.boost.org/mailman/listinfo.cgi/boost-users |
|
|
Re: naming delayed variables string in boost lamdba
OvermindDL1 wrote:
Could you translate the example to use BOOST_AUTO in place of Boost.Function?On Wed, Nov 11, 2009 at 9:24 AM, Roman Perepelitsa roman.perepelitsa@... wrote:2009/11/11 Emanuele Rocci rocciemanuele@... Ryan _______________________________________________ Boost-users mailing list Boost-users@... http://lists.boost.org/mailman/listinfo.cgi/boost-users |
|
|
Re: naming delayed variables string in boost lamdbaThanks for your nice response! I have just discovered another way. Please give me your comments! constant_type< const char[7] >::type _name(constant("value: ")); playing the same role of boost::function<const char*()> _name = boost::lambda::constant("value: ");I find both 2 nice ways of expressing constant string in a lamda expression. std::for_each( myList.begin(),myList.end(),(
std::cout << _name << _1 ,
_1 = 2
));Date: Wed, 11 Nov 2009 17:24:35 +0100 From: roman.perepelitsa@... To: boost-users@... Subject: Re: [Boost-users] naming delayed variables string in boost lamdba 2009/11/11 Emanuele Rocci <rocciemanuele@...>
Function objects generated by lambda have unspecified types. If you want to save them, use Boost.Function.
#include <iostream> #include <boost/lambda/core.hpp>
#include <boost/function.hpp>
int main() { boost::function<const char*()> f = boost::lambda::constant("hello");
std::cout << f() << std::endl; }
Roman Perepelitsa Il tormentone dell'estate? Riascoltalo sulla Messenger Radio _______________________________________________ Boost-users mailing list Boost-users@... http://lists.boost.org/mailman/listinfo.cgi/boost-users |
|
|
Re: naming delayed variables string in boost lamdba2009/11/11 Emanuele Rocci <rocciemanuele@...>
This does not do what you think it does. It'll evaluate and output _name() only once. Here's how you can fix it:
std::for_each( myList.begin(),myList.end(),( std::cout << bind(_name) << _1 ,
_1 = 2 ));
This works as expected -- _name is being output several times.
Using constant_type should be faster, because it does not use virtual functions. Also, to my surprise, it's a documented way of creating lazy constants (see http://www.boost.org/doc/libs/1_40_0/doc/html/lambda/le_in_details.html#lambda.delaying_constants_and_variables).
Roman Perepelitsa. _______________________________________________ Boost-users mailing list Boost-users@... http://lists.boost.org/mailman/listinfo.cgi/boost-users |
| Free embeddable forum powered by Nabble | Forum Help |