|
Boost Users : |
Subject: Re: [Boost-users] naming delayed variables string in boost lamdba
From: Emanuele Rocci (rocciemanuele_at_[hidden])
Date: 2009-11-11 13:23:15
HI Roman!Thanks 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 ofboost::function<const char*()> _name = boost::lambda::constant("value: ");I find both 2 nice ways of expressing constant string in a lamda expression.
Using the boost::function approach, some sample code can be written like the following
using round brackets next to _name variable.std::for_each( myList.begin(),myList.end(),(
std::cout << _name() << _1 ,
_1 = 2
));I find the approach using boost::function very nice since it provides
more the "genericity" feeling; you don't have to declare the size of the
string but it is enough const char*().With the constant_type way it is possible to use _name as a common const variable without
using round bracketsstd::for_each( myList.begin(),myList.end(),(
std::cout << _name << _1 ,
_1 = 2
));This works fine provided that the size is correct; it is probably not always nice,
but compiler will complain if the size is not correct i.e. if you use const char[6]
or if you use const char[8].Now I am wondering which is the fastest approach. I guess that probably they are
the same.
You commnnts or opinion are appreciated and off course thanks!ManuDate: Wed, 11 Nov 2009 17:24:35 +0100
From: roman.perepelitsa_at_[hidden]
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] naming delayed variables string in boost lamdba
2009/11/11 Emanuele Rocci <rocciemanuele_at_[hidden]>
Hi AllI 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 everythingis 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 advanceMn
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
_________________________________________________________________
Scrivi, parla, gioca... Scopri Messenger 2009
http://www.messenger.it/home_comunica.aspx
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net