Reputation: 26983
I'm trying to make a implicit lambda convertible to lambda function by the following code:
#include <boost/function.hpp>
struct Bla {
};
struct Foo {
boost::function< void(Bla& )> f;
template <typename FnType>
Foo( FnType fn) : f(fn) {}
};
#include <iostream>
int main() {
Bla bla;
Foo f( [](Bla& v) -> { std::cout << " inside lambda " << std::endl; } );
};
However, i'm getting this error with g++
$ g++ --version
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
$ g++ -std=c++0x test.cpp `pkg-config --cflags boost-1.46` -o xtest `pkg-config --libs boost-1.46`
test.cpp: In function ‘int main()’:
test.cpp:21: error: expected primary-expression before ‘[’ token
test.cpp:21: error: expected primary-expression before ‘]’ token
test.cpp:21: error: expected primary-expression before ‘&’ token
test.cpp:21: error: ‘v’ was not declared in this scope
test.cpp:21: error: expected unqualified-id before ‘{’ token
any idea how can i achieve the above? or if i can achieve it at all?
UPDATE trying with g++ 4.5
$ g++-4.5 --version
g++-4.5 (Ubuntu/Linaro 4.5.1-7ubuntu2) 4.5.1
$ g++-4.5 -std=c++0x test.cpp `pkg-config --cflags boost-1.46` -o xtest `pkg-config --libs boost-1.46`
test.cpp: In function ‘int main()’:
test.cpp:20:22: error: expected type-specifier before ‘{’ token
Upvotes: 0
Views: 4825
Reputation: 39228
You are missing a void
:
Foo f( [](Bla& v) -> void { std::cout << " inside lambda " << std::endl; } );
// ^ here
Or, as @ildjarn pointed out, you can simply omit the return type:
Foo f( [](Bla& v) { std::cout << " inside lambda " << std::endl; } );
With either of these lines, your code compiles fine using MinGW g++ 4.5.2 and Boost v1.46.1.
Upvotes: 3
Reputation: 88225
Your lambda syntax is wrong. you have the '->' in there but don't specify the return type. You probably mean:
Foo f( [](Bla& v) { std::cout << " inside lambda " << std::endl; } );
Upvotes: 3