【问题标题】:Is there a single-input, binary output stdlib or boost function I can bind to?是否有可以绑定的单输入、二进制输出 stdlib 或 boost 函数?
【发布时间】:2017-02-24 23:50:27
【问题描述】:
bool all_ok = boost::algorithm::all_of(info.begin(), threadInfo.end(), [&](std::pair<std::string, Info> const &p){ return p.second.am_ok; });

上面是我试图从中删除 c++11 的一行(为了符合我正在集成的应用程序,如果你必须知道的话)。我想在不定义当前方法外部的函数的情况下替换 lambda。

我的问题是,我如何利用 boost::bind 来表示一个函数和绑定,它接受单个输入并返回一个布尔值?

【问题讨论】:

标签: c++ boost


【解决方案1】:

如果你真的想使用boost::bind 来做这件事,它看起来像这样:

boost::bind(&Info::am_ok, boost::bind(&std::pair<std::string, Info>::second, _1));

Boost.Bind 支持其函数对象的组合,因此在这种情况下,由嵌套的bind 创建的函数对象首先被调用,其结果(Info 对象)被传递给封闭的函数对象,它返回结构的am_ok 成员。

您看到代码有些做作,因此您可能还是想编写一个自定义函数对象。

struct is_ok
{
    typedef bool result_type;
    result_type operator() (std::pair<std::string, Info> const &p) const
    {
        return p.second.am_ok;
    }
};

【讨论】:

    猜你喜欢
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2017-11-29
    • 2016-06-17
    • 1970-01-01
    相关资源
    最近更新 更多