1

以下是我经常使用的可爱小模板。简单地告诉我给定元素是否是集合的成员(它本身必须与 find_if 模板兼容):

// returns true if a given collection contains the given element
// NOTE: This is NOT optimized for associative containers!
template <typename ELEMENT, typename COLLECTION, typename PREDICATE>
bool contains(const COLLECTION & collection, ELEMENT element, PREDICATE predicate)
{
    return collection.end() != std::find_if(collection.begin(), collection.end(), boost::bind(predicate, element, _1));
}

如果我尝试使用 lambda 作为谓词,我发现 VC2012 会犹豫:

if (!contains(specs, str, [] (CString pathname, CString pattern) { return AsBool(PathMatchSpec(pathname, pattern)); }))
    continue;

VS2012SP1 为上述上下文吐出以下内容:

1>c:\users\steve\projects\cimex cad-cam\15.0\3rd party\boost\boost\bind\bind.hpp(69): error C2039: 'result_type' : is not a member of 'CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>'
1>          c:\users\steve\projects\cimex cad-cam\15.0\cimex application\cimcad\macro directory.cpp(166) : see declaration of 'CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>'
1>          c:\users\steve\projects\cimex cad-cam\15.0\3rd party\boost\boost\bind\bind_template.hpp(15) : see reference to class template instantiation 'boost::_bi::result_traits<R,F>' being compiled
1>          with
1>          [
1>              R=boost::_bi::unspecified,
1>              F=CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>
1>          ]
1>          c:\users\steve\projects\cimex cad-cam\15.0\mfc toolbox\miscellaneous.h(360) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1>          with
1>          [
1>              R=boost::_bi::unspecified,
1>              F=CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>,
1>              L=boost::_bi::list2<boost::_bi::value<CString>,boost::arg<1>>
1>          ]
1>          c:\users\steve\projects\cimex cad-cam\15.0\cimex application\cimcad\macro directory.cpp(166) : see reference to function template instantiation 'bool contains<CString,substring_container_adapter,CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>>(const COLLECTION &,ELEMENT,PREDICATE)' being compiled
1>          with
1>          [
1>              COLLECTION=substring_container_adapter,
1>              ELEMENT=CString,
1>              PREDICATE=CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>
1>          ]

我不清楚如何强迫事物接受谓词 lambda。似乎 boost 无法推断出 lambda 的返回类型。我不清楚我能做些什么来解决这个问题?

我可以定义一个本地 std::binary_function 导数函子。似乎修复 contains<> 以允许它直接处理 lambda 会更好。

4

2 回答 2

5

这似乎是一个问题boost::bind。相反,使用std::bindVS2012 中的 lambda,您的代码可以很好地构建:

#include <functional>
#include <algorithm>
#include <vector>

template <typename ELEMENT, typename COLLECTION, typename PREDICATE>
bool contains(const COLLECTION & collection, ELEMENT element, PREDICATE predicate)
{
    return collection.end() != std::find_if(collection.begin(), collection.end(), std::bind(predicate, element, std::placeholders::_1));
}

std::vector<int> a;

int main()
{
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    a.push_back(42);
    bool c = contains(a, 42, [](int a, int b) { return a == b; });
    return 0;
}

同样的代码也可以很好地构建g++

于 2013-01-18T22:01:14.020 回答
2

您总是可以尝试使用另一个 lambda:

template <typename ELEMENT, typename COLLECTION, typename PREDICATE>
bool contains(const COLLECTION & collection, ELEMENT element, PREDICATE predicate)
{
    typedef typename COLLECTION::value_type VALUE;

    return collection.end() != std::find_if(collection.begin(), collection.end(),
      [&]( VALUE const & e ){ return predicate( element, e ); });
}
于 2013-01-18T22:05:50.933 回答