小编Jef*_*ler的帖子

使用boost :: bind来传递比预期更安全的参数吗?

使用,生成的可能会收到比绑定对象所期望的更多的参数.概念:

int func() { return 42; }
boost::function<int (int,int,int)> boundFunc = boost::bind(&func);
int answer = boundFunc(1,2,3);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,func()即使其签名表明它不带参数,也会在堆栈上接收1,2和3.

这不同于更典型的使用boost::bind用于局部应用,其中的值是固定的,产生了一定的对象boost::function即需要更少的参数,但是提供正确的参数数调用绑定对象时.

以下代码适用于MSVC++ 2010 SP1.这是一种简化形式的帖子; 原始代码也适用于Linux上的g ++ 4.4.

以下是根据C++标准明确定义的吗?

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

using namespace std;

void func1(int x) { std::cout << "func1(" << x << ")\n"; } // end func1()

void func0() { std::cout << "func0()\n"; } // end func0()

int main(int argc,char* argv[]) 
{
        typedef boost::function<void …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-bind boost-function

24
推荐指数
1
解决办法
5063
查看次数

标签 统计

boost ×1

boost-bind ×1

boost-function ×1

c++ ×1