boost lambda expression

Discussion in 'C++' started by rammel, Apr 25, 2007.

  1. rammel

    rammel Guest

    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<boost::lambda::placeholder<1>
    >'


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

    -----------------------------------------------------------

    Thanks for any idea!
     
    rammel, Apr 25, 2007
    #1
    1. Advertising

  2. rammel

    dasjotre Guest

    use lambda::bind

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

    or specialize ostream::eek:perator<< for string
     
    dasjotre, Apr 26, 2007
    #2
    1. Advertising

  3. rammel

    Salt_Peter Guest

    On Apr 25, 5:08 pm, rammel <> wrote:
    > 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 <string>

    > #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< std::string > v1(10, "haha");

    > 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;
     
    Salt_Peter, Apr 26, 2007
    #3
  4. rammel

    dasjotre Guest

    On 26 Apr, 13:25, dasjotre <> wrote:
    > 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 &)
     
    dasjotre, Apr 30, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Suzi
    Replies:
    13
    Views:
    609
    Bengt Richter
    Jan 7, 2005
  2. Toby Bradshaw
    Replies:
    6
    Views:
    1,767
    Kai-Uwe Bux
    Jun 2, 2006
  3. Misiu
    Replies:
    3
    Views:
    2,394
    Misiu
    Jan 31, 2007
  4. Replies:
    0
    Views:
    595
  5. nudnik
    Replies:
    2
    Views:
    416
    Juha Nieminen
    Sep 28, 2012
Loading...

Share This Page