cannot compile a simple boost lambda expression


N

nudnik

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 ?
 
Ad

Advertisements

S

Stefan van Kessel

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
 
Ad

Advertisements

J

Juha Nieminen

nudnik said:
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?
 

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

Top