cannot compile a simple boost lambda expression

Discussion in 'C++' started by nudnik, Sep 27, 2012.

  1. nudnik

    nudnik Guest

    I tried recently to use boost lambda library but I cannot manage to compile following code


    #include <boost/lambda/lambda.hpp>
    #include <boost/lambda/bind.hpp>
    #include <boost/lambda/core.hpp>

    using namespace boost::lambda;

    #include <iostream>

    template <typename F>
    void call ( F f ) {
    f();
    }

    int main (int, char**) {
    void (*f)();

    //call ([] () { std::cout << "hello world\n"; } );
    // auto my_lambda_func = [&](int x) {};

    f = [] () { std::cout << "hello world\n"; } ;
    call (f);
    }

    What ever compiler I use without switching to c++0x [ -std=c++0x ] complains with following errors:

    lambda.cc:21: error: expected primary-expression before ?[? token
    lambda.cc:21: error: expected primary-expression before ?]? token
    lambda.cc:21: error: expected `;' before ?{? token


    and this even with a newer compiler gcc 4.4.x


    My command line looks like g++ -I boost_directory -c input.cpp

    When I switch to version 4.6.1, compile after removing the lambda includes and add an option -std=c++0x it works out fine.
    What mistake did I make ?
    nudnik, Sep 27, 2012
    #1
    1. Advertising

  2. Boost.lambda doesn't offer the same kind of syntax and not all of the
    features of c++11 lambdas. The new lambdas are a core language feature
    that is impossible to implement the same way with a library.

    Here is one way, though not necessarily a pretty one, to do what you
    tried to do with boost.lambda.


    #include <boost/lambda/lambda.hpp>
    #include <boost/lambda/bind.hpp>
    #include <boost/lambda/core.hpp>
    #include <boost/bind.hpp>
    #include <boost/function.hpp>

    #include <iostream>

    template <typename F>
    void call ( F f ) {
    f();
    }

    int main (int, char**) {
    boost::function<void ()> f;
    f = boost::bind<void>(
    std::cout << boost::lambda::_1 << '\n',
    "hello world\n");
    call (f);
    }

    The documentation for boost.lambda can be found at
    http://www.boost.org/doc/libs/1_51_0/doc/html/lambda.html

    On 2012-09-27 11:04, nudnik wrote:
    > I tried recently to use boost lambda library but I cannot manage to compile following code
    >
    >
    > #include<boost/lambda/lambda.hpp>
    > #include<boost/lambda/bind.hpp>
    > #include<boost/lambda/core.hpp>
    >
    > using namespace boost::lambda;
    >
    > #include<iostream>
    >
    > template<typename F>
    > void call ( F f ) {
    > f();
    > }
    >
    > int main (int, char**) {
    > void (*f)();
    >
    > //call ([] () { std::cout<< "hello world\n"; } );
    > // auto my_lambda_func = [&](int x) {};
    >
    > f = [] () { std::cout<< "hello world\n"; } ;
    > call (f);
    > }
    Stefan van Kessel, Sep 27, 2012
    #2
    1. Advertising

  3. nudnik <> wrote:
    > I tried recently to use boost lambda library but I cannot manage to
    > compile following code


    Did you even check the Boost documentation for the correct syntax?
    Juha Nieminen, Sep 28, 2012
    #3
    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. Toby Bradshaw
    Replies:
    6
    Views:
    1,716
    Kai-Uwe Bux
    Jun 2, 2006
  2. Misiu
    Replies:
    3
    Views:
    2,344
    Misiu
    Jan 31, 2007
  3. Replies:
    4
    Views:
    405
  4. rammel

    boost lambda expression

    rammel, Apr 25, 2007, in forum: C++
    Replies:
    3
    Views:
    472
    dasjotre
    Apr 30, 2007
  5. Replies:
    0
    Views:
    574
Loading...

Share This Page