如何使用C++11 lambda作为boost谓词?

如何使用C++11 lambda作为boost谓词?,c++,boost,c++11,lambda,C++,Boost,C++11,Lambda,我想使用单个分隔符将wstring拆分为向量。 此字符在头文件中定义为单个字符。 为了保持代码的干净性和可读性,我真的希望在一行中这样做: 我找不到要使用的谓词,所以我决定使用C++11 lambda #include <boost/algorithm/string/split.hpp> #include <vector> #include <string> constexpr char separator = '.'; // Th

我想使用单个分隔符将wstring拆分为向量。 此字符在头文件中定义为单个字符。 为了保持代码的干净性和可读性,我真的希望在一行中这样做: 我找不到要使用的谓词,所以我决定使用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 lambdas不完全正确?支持boost

是否有另一种可读的单线解决方案

我目前正在使用一个自己的谓词is_char,它是我在一些基本库中定义的,但我宁愿放弃它

我知道BoostLambdas还没有使用它们,但它们真的应该在C++11代码中使用吗


谢谢

在一个分支上,在包含第一个boost头之前尝试定义它,注意预编译头:

#define BOOST_RESULT_OF_USE_DECLTYPE
或者相反地

#define BOOST_RESULT_OF_USE_TR1

我相信违约已经改变了。不久前对于特定的编译器,这可以很好地解释它。

什么版本的boost?boost的一些旧版本与c++11不兼容,因为它是boost 1.53。chris?AFAIR,你使用哪个版本的boost和c++编译器,将lambda强制转换为函数指针,只有当它没有捕获任何内容时才有效,当然对我有效。我现在尝试使用boost 1.55和clang 3.3。同样的问题。所以我猜一定是叮当作响。当我有机会使用Clang3.4或3.5进行测试时,我将在这里报告结果。这可能需要一些时间。谢谢。不幸的是,这并没有改变任何事情。