如何使用 C++11 λ 作为提升谓词
How to use C++11 lambda as boost predicate?
我想使用单个分隔符将wstring
拆分为vector<wstring>
。此字符在头文件中定义为单个char
。为了保持代码干净易读,我真的很想在一行:)我找不到要使用的谓词,所以我决定使用 C++11 lambda。
#include <boost/algorithm/string/split.hpp>
#include <vector>
#include <string>
constexpr char separator = '.'; // This is how it's declared in some header file
int main()
{
std::wstring text( L"This.is.a.test" );
std::vector<std::wstring> result;
// can't use is_any_of() unless i convert it to a wstring first.
boost::algorithm::split( result, text, [](wchar_t ch) -> bool { return ch == (wchar_t) separator; });
return 0;
}
不幸的是,这会导致编译错误(clang 3.3):
clang++ -c -pipe -fPIC -g -std=c++11 -Wextra -Wall -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -I/usr/include -I/usr/lib64/qt5/mkspecs/linux-clang -I../splittest -I. -o debug/main.o ../splittest/main.cpp
In file included from ../splittest/main.cpp:1:
In file included from /usr/include/boost/algorithm/string/split.hpp:16:
/usr/include/boost/algorithm/string/iter_find.hpp:148:13: error: non-type template argument refers to function 'failed' that does not have linkage
BOOST_CONCEPT_ASSERT((
^~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/concept/assert.hpp:44:5: note: expanded from macro 'BOOST_CONCEPT_ASSERT'
BOOST_CONCEPT_ASSERT_FN(void(*)ModelInParens)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/concept/detail/general.hpp:70:6: note: expanded from macro 'BOOST_CONCEPT_ASSERT_FN'
&::boost::concepts::requirement_<ModelFnPtr>::failed>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/algorithm/string/split.hpp:146:40: note: in instantiation of function template specialization 'boost::algorithm::iter_split<std::vector<std::basic_string<wchar_t>, std::allocator<std::basic_string<wchar_t> > >, std::basic_string<wchar_t>, boost::algorithm::detail::token_finderF<<lambda at ../splittest/main.cpp:13:44> > >' requested here
return ::boost::algorithm::iter_split(
^
../splittest/main.cpp:13:23: note: in instantiation of function template specialization 'boost::algorithm::split<std::vector<std::basic_string<wchar_t>, std::allocator<std::basic_string<wchar_t> > >, std::basic_string<wchar_t>, <lambda at ../splittest/main.cpp:13:44> >' requested here
boost::algorithm::split( result, text, [](wchar_t ch) -> bool { return ch == (wchar_t) separator; });
^
/usr/include/boost/concept/detail/general.hpp:46:17: note: non-type template argument refers to function here
static void failed() { ((Model*)0)->constraints(); }
^
1 error generated.
我做错了什么还是 C++11-lambda 在 boost 中不受(完全?
有没有另一种可读的单行解决方案?
我目前正在使用我在某些基本库中定义的自己的谓词is_char()
,但我宁愿摆脱它。
我知道 boost lambdas(还没有使用过它们) - 但它们真的应该在 C++11 代码中使用吗?
谢谢!
在包含第一个 boost 标头之前,请尝试定义以下内容(当心预编译标头):
#define BOOST_RESULT_OF_USE_DECLTYPE
或者 - 相反
#define BOOST_RESULT_OF_USE_TR1
我相信默认值已经改变。最近。对于特定的编译器,这可以很好地解释它。
相关文章:
- 尝试使用谓词函数会导致错误:"std::sort"未找到匹配的重载函数
- 如何在算法中使用谓词函数find_if?
- 是否可以使用三元切换/控制/谓词成员函数调用?
- 为什么以及如何在 C++ 中使用 bind() 作为谓词?
- 如何正确使用 std::function 作为谓词
- 使用boost :: mpl :: find_if带有自定义谓词
- C++ STL 使用比较谓词/函子与容器
- 使用 std::find 和用户提供的谓词 - 自制的 find() 重载
- 在条件变量中不使用谓词会导致速度变慢
- 在 O(N) 时间内使用布尔谓词函数对向量进行排序
- 使用迭代器按谓词重新排列
- 使用成员变量作为谓词
- 如何使用 C++11 λ 作为提升谓词
- 是否有使用谓词比较两个范围的标准方法
- 在C++中使用排序谓词
- 使用 LLVM 的 libc++ 时,__1 符号从何而来?
- 是否可以使用给定参数为结构创建推力的功能谓词?
- 在std::cout之后使用std::cin时,换行符从何而来
- 如何在一元谓词中使用类成员变量
- 理解sort和binary_function中谓词的使用