【问题标题】:Functional programming with boost: pass a boost::bind to a boost::bind使用 boost 进行函数式编程:将 boost::bind 传递给 boost::bind
【发布时间】:2014-06-11 18:27:18
【问题描述】:

我正在尝试对我正在编写的一些代码采用功能性方法。特别是,我想将一个函数传递给另一个对其执行某些操作的函数,然后在boost::asio::io_service 上安排后一个函数。

我尝试了以下方法(受io_service.hppio_service::post 的定义启发)为template <typename CompletionHandler> void post(CompletionHandler handler);):

#include <stdio.h>

#include <boost/asio/io_service.hpp>
#include <boost/bind.hpp>

template <typename FUNC>
void foo_then_smile(FUNC handler) {
    handler();
    printf("Smile!\n");
}

void foo(int a, int b, int c) {
    printf("The important answer is %d\n", a + b + c);
}

int main() {
    boost::asio::io_service ioService;

    ioService.post(boost::bind(foo_then_smile, boost::bind(foo, 1, 2, 3)));

    return 0;
}

但我明白了:

no matching function for call to 'bind(<unresolved overloaded function type>, boost::_bi::bind_t<void, void (*)(int, int, int), boost::_bi::list3<boost::_bi::value<int>, boost::_bi::value<int>, boost::_bi::value<int> > >)'

紧随其后的是大量候选人。

【问题讨论】:

    标签: c++ function boost functional-programming boost-bind


    【解决方案1】:

    问题是您尝试绑定到模板函数,但该函数不起作用。您可以通过将 foo_then_smile 替换为 here 所述的仿函数来解决此问题。

    但是为了它的价值;如果您使用的编译器支持 C++11,您通常可以使用 lambdas 代替绑定。我发现它们的语法更简洁,更易读,它们会让你彻底解决你遇到的问题:

    ioService.post([]() { foo_then_smile( []() { foo(1, 2, 3); } ); });

    【讨论】:

    • 从技术上讲,这不是在回答问题。它没有说明如何将绑定表达式传递给绑定表达式。
    • 问题不在于将绑定传递给绑定;它绑定到模板。我包含的链接解释了如何处理。
    • 不过,问题是关于将绑定传递给绑定。我同意还有更多问题,但这并没有使问题神奇地改变:)
    • 我读到的问题是“为什么不能编译”。 :) 我试图为根本问题提供两种不同的解决方案。
    • +1 来自我。您发布的链接与 sehe 具有基本相同的解决方案,尽管 sehe 确实提供了一个完整的工作示例,非常感谢!
    【解决方案2】:

    而且如果不能使用c++11,可以把函数模板变成多态函数对象:

    struct foo_then_smile
    {
        typedef void result_type;
    
        template <typename FUNC>
            void operator()(FUNC handler) const {
                handler();
                printf("Smile!\n");
            }
    };
    

    并使用它(注意protect 以避免混淆绑定展开):

    ioService.post(boost::bind(foo_then_smile(), 
                   boost::protect(boost::bind(foo, 1, 2, 3))));
    

    Live On Coliru

    #include <stdio.h>
    
    #include <boost/asio/io_service.hpp>
    #include <boost/bind.hpp>
    #include <boost/bind/protect.hpp>
    
    struct foo_then_smile
    {
        typedef void result_type;
    
        template <typename FUNC>
            void operator()(FUNC handler) const {
                handler();
                printf("Smile!\n");
            }
    };
    
    void foo(int a, int b, int c) {
        printf("The important answer is %d\n", a + b + c);
    }
    
    int main() {
        boost::asio::io_service ioService;
    
        ioService.post(boost::bind(foo_then_smile(), boost::protect(boost::bind(foo, 1, 2, 3))));
    
        ioService.run();
    }
    

    打印:

    The important answer is 6
    Smile!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 2023-04-07
      • 1970-01-01
      • 2013-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多