隐式地将lambda转换为boost::函数
implicitly converting lambda to boost::function
我试图通过以下代码使隐式lambda转换为lambda函数:
#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; } );
};
然而,我得到这个错误与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
你知道我怎样才能实现上述目标吗?或者我能不能做到?
UPDATE try 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
您缺少一个void
:
Foo f( [](Bla& v) -> void { std::cout << " inside lambda " << std::endl; } );
// ^ here
或者,正如@ildjarn指出的,您可以简单地省略返回类型:
Foo f( [](Bla& v) { std::cout << " inside lambda " << std::endl; } );
使用这两行代码中的任何一行,您的代码都可以很好地使用mingwg++ 4.5.2和Boost v1.46.1编译。
您的lambda语法错误。这里有'->'但没有指定返回类型。你的意思可能是:
Foo f( [](Bla& v) { std::cout << " inside lambda " << std::endl; } );
相关文章:
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- Boost buffer_cast 无法从 void* 转换为 PointerToPodType
- Boost.Spirit将表达转换为AST
- 将 boost 序列化对象的 asio::streambuf 表示转换为 Beast 的 DynamicBody req.body()
- 将"boost::tuple "转换为"boost::fusion::tuple&quo
- 用于 boost::p roperty_tree 的通用枚举转换器
- 如何将 boost::hana::map 转换为 lambda
- 如何将 boost::hana::tuple 转换为 std::变体
- 将 boost::文件系统::p ath 转换为 char?
- Boost.Hana:在 constexpr 上下文中将值元组转换为相应类型的元组
- 为什么要 boost::p roperty_tree::write_json() 将整数值转换为字符串?这是不正确的
- 如何正确地将boost::asio::buffers_iterator转换为InputIterator
- 如何将boost::asio::ip::address_v6 ip转换为2个uint64_t数字,并从2个uint 64
- 如何将 boost::beast 的响应<buffer_body>转换为响应<string_body>?
- 为boost::dynamic_bitset生成哈希,并将哈希转换回boost::dynamic_bitset
- 如何将 time-uuid(存储在 boost uuid 中)转换为时间戳/自纪元以来的时间?
- 将 boost:::chrono::steady_clock::time_point 转换为标准::时间::steady
- n-ary布尔语法从中缀到前缀的Boost::Spirit转换?
- 寻找Boost转换迭代器的复合特征模式
- 使用boost转换度、分、秒、弧度