boost lambda expression

R

rammel

hi,

can't I call member functions from an placeholder of a boost-like
lambda expression?
look on the example below please:

-----------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <boost/lambda/lambda.hpp>

using namespace std;
using namespace boost::lambda;

int main(int argc, char **argv)
{
// this works:

vector<string> v1(10);
fill(v1.begin(), v1.end(), "haha");
for_each(v1.begin(), v1.end(), cout << _1 << " ");
cout << endl;

// this works, too:

vector<string*> v2(10);
fill(v2.begin(), v2.end(), new string("hehe"));
for_each(v2.begin(), v2.end(), cout << *_1 << " ");
cout << endl;

// this doesn't:

vector<string> v3(10);
fill(v3.begin(), v3.end(), "hoho");
for_each(v3.begin(), v3.end(), cout << _1->c_str() << " ");
// error: base operand of '->' has non-pointer type 'const
// boost::lambda::lambda_functor said:

cout << v3.begin()->c_str() << endl; // this works, just to be
sure
cout << endl;
 
D

dasjotre

use lambda::bind

for_each(v3.begin(), v3.end(), cout << bind(&string::c_str, _1) << "
");

or specialize ostream::eek:perator<< for string
 
S

Salt_Peter

hi,

can't I call member functions from an placeholder of a boost-like
lambda expression?
look on the example below please:

-----------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>

#include said:
#include <boost/lambda/lambda.hpp>

using namespace std;
using namespace boost::lambda;

int main(int argc, char **argv)
{
// this works:

vector<string> v1(10);

std::vector said:
fill(v1.begin(), v1.end(), "haha");
for_each(v1.begin(), v1.end(), cout << _1 << " ");
cout << endl;

// this works, too:

vector<string*> v2(10);
fill(v2.begin(), v2.end(), new string("hehe"));

// no its doesn't work, all the pointers are the same
// why? look at std::fill(...) or better yet - print the
pointers
for_each(v2.begin(), v2.end(), cout << *_1 << " ");
cout << endl;

// this doesn't:

vector<string> v3(10);
fill(v3.begin(), v3.end(), "hoho");
for_each(v3.begin(), v3.end(), cout << _1->c_str() << " ");
// error: base operand of '->' has non-pointer type 'const
// boost::lambda::lambda_functor<boost::lambda::placeholder<1>

int main()
{
std::vector< std::string > v1(10, "haha");
std::for_each(v1.begin(), v1.end(), std::cout << _1 << " ");
std::cout << std::endl;

std::vector< std::string* > vp(10);
// sets the pointers in vp to the addresses of v1's objects
// caution: as long as v1 isn't resized
std::transform(v1.begin(), v1.end(), vp.begin(), &_1);
std::for_each(vp.begin(), vp.end(), std::cout << _1 << "\n");
std::cout << std::endl;
}

haha haha haha haha haha haha haha haha haha haha
0x502040
0x502048
0x502050
0x502058
0x502060
0x502068
0x502070
0x502078
0x502080
0x502088

Why not just overload a global templated op<< for a std::vector?

std::cout << v1 << std::endl;
 
D

dasjotre

use lambda::bind

for_each(v3.begin(), v3.end(), cout << bind(&string::c_str, _1) << "
");

or specialize ostream::eek:perator<< for string

I meant: write ostream & operator << (ostream &, string const &)
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top