boost lambda binding question

Discussion in 'C++' started by baliga@gmail.com, Jan 26, 2007.

  1. Guest

    (std::cout << _1 << _2)("hello", 10);

    This does not work. If 2 parameters are string, it works. If any one or
    both parameters are integers, it does not work.

    But If I change it to

    boost::function<void(int, int)> f = std::cout << _1 << _2;
    f(10, 10);

    it works.

    Question is why the first one does not work when one of the parameter
    is integer.

    Thanx,
    -- baliga

    http://baliga.blogdns.com/blog
    , Jan 26, 2007
    #1
    1. Advertising

  2. shunsuke Guest

    On Jan 26, 9:50 am, wrote:
    > (std::cout << _1 << _2)("hello", 10);
    >
    > This does not work. If 2 parameters are string, it works. If any one or
    > both parameters are integers, it does not work.
    >
    > But If I change it to
    >
    > boost::function<void(int, int)> f = std::cout << _1 << _2;
    > f(10, 10);
    >
    > it works.
    >
    > Question is why the first one does not work when one of the parameter
    > is integer.


    Hi,

    Lambda Functor can't accept a non-const rvalue(temporary).
    See..
    http://www.boost.org/doc/html/lambda/le_in_details.html#lambda.rvalues_as_actual_arguments
    http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1385.htm

    Thus,
    (std::cout << _1 << _2)("hello", make_const(10));
    works fine.

    --
    Shunsuke Sogame
    shunsuke, Jan 26, 2007
    #2
    1. Advertising

  3. Guest

    On Jan 25, 8:25 pm, "shunsuke" <> wrote:
    > On Jan 26, 9:50 am, wrote:
    >
    > > (std::cout << _1 << _2)("hello", 10);

    >
    > > This does not work. If 2 parameters are string, it works. If any one or
    > > both parameters are integers, it does not work.

    >
    > > But If I change it to

    >
    > > boost::function<void(int, int)> f = std::cout << _1 << _2;
    > > f(10, 10);

    >
    > > it works.

    >
    > > Question is why the first one does not work when one of the parameter
    > > is integer.Hi,

    >
    > Lambda Functor can't accept a non-const rvalue(temporary).
    > See..http://www.boost.org/doc/html/lambd....dk/jtc1/sc22/wg21/docs/papers/2002/n1385.htm
    >
    > Thus,
    > (std::cout << _1 << _2)("hello", make_const(10));
    > works fine.
    >
    > --
    > Shunsuke Sogame


    Thanx for the solution. But I still have one question. why
    make_const("hello") is not required for the string or is there any
    implicit conversion going on within boost?
    , Jan 26, 2007
    #3
  4. shunsuke Guest

    "reference to array" kicks in.
    The reference seems passed to 'operator<<' as is.
    Then, the 'operator<<' overload of 'char const*' is selected, AFAIK.


    Regards,

    --
    Shunsuke Sogame


    On Jan 27, 4:08 am, wrote:
    > On Jan 25, 8:25 pm, "shunsuke" <> wrote:
    >
    >
    >
    >
    >
    > > On Jan 26, 9:50 am, wrote:

    >
    > > > (std::cout << _1 << _2)("hello", 10);

    >
    > > > This does not work. If 2 parameters are string, it works. If any one or
    > > > both parameters are integers, it does not work.

    >
    > > > But If I change it to

    >
    > > > boost::function<void(int, int)> f = std::cout << _1 << _2;
    > > > f(10, 10);

    >
    > > > it works.

    >
    > > > Question is why the first one does not work when one of the parameter
    > > > is integer.Hi,

    >
    > > Lambda Functor can't accept a non-const rvalue(temporary).
    > > See..http://www.boost.org/doc/html/lambda/le_in_details.html#lambda.rvalue...

    >
    > > Thus,
    > > (std::cout << _1 << _2)("hello", make_const(10));
    > > works fine.

    >
    > > --
    > > Shunsuke SogameThanx for the solution. But I still have one question. why

    > make_const("hello") is not required for the string or is there any
    > implicit conversion going on within boost?- Hide quoted text -- Show quoted text -
    shunsuke, Jan 27, 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:
    593
    Bengt Richter
    Jan 7, 2005
  2. Toby Bradshaw
    Replies:
    6
    Views:
    1,724
    Kai-Uwe Bux
    Jun 2, 2006
  3. Misiu
    Replies:
    3
    Views:
    2,353
    Misiu
    Jan 31, 2007
  4. Replies:
    0
    Views:
    580
  5. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    157
    Sean O'Halpin
    Mar 30, 2009
Loading...

Share This Page