首页 文章

如何将参数绑定到boost :: function?

提问于
浏览
0

来自boost :: bind docs(http://www.boost.org/doc/libs/1_53_0/libs/bind/bind.html#with_functions),"The arguments that bind takes are copied and held internally by the returned function object",但如果有办法我可以在这些函数对象中复制参数?

即:

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <string>

using namespace std;

void doSomthing(std::string str)
{
}

int main()
{    
    boost::function<void(void)> func_obj = boost::bind(&doSomthing, "some string");
    //how can I get the std::string argument("some string") through func_obj?
}

提前致谢 .

1 回答

  • 0

    除了调用它之外,Boost.Function对象并没有什么用处 - 而且这是设计的 . (你可以复制它,销毁它,比较为NULL,但不是更多) .

    请考虑以下代码:

    void Foo () {}
    void Bar ( int i ) { printf ( "%d", i ); }
    
    boost::function<void(void)> fFoo (Foo);
    boost::function<void(void)> fBar = boost::bind (Bar, 23);
    

    这两个对象被设计为相同的处理 . 它们是相同的类型,并且表现相同 . 在增强功能中没有用于区分它们的机制 .

    有关Boost.Function(和其他地方)使用的技术的详细描述,请查看Nevin Liber的type erasure talk from Boostcon 2010

相关问题