标签: boost-bind

如果函数指针不需要&符号,为什么boost :: bind需要一个?

我一直认为函数指针不需要&符号:

函数指针需要一个&符号

然而,我所看到的每个使用的例子都boost::bind显示了一个,而我的编译器 - 在大多数情况下 - 如果省略则会给出一个通常难以理解的错误信息.

synchronize(boost::bind(&Device::asyncUpdate , this, "ErrorMessage"));  // Works

synchronize(boost::bind(Device::asyncUpdate , this, "ErrorMessage"));   // Fails
Run Code Online (Sandbox Code Playgroud)

假设boost::bind第一个参数基本上是函数指针,我错了吗?

c++ function-pointers boost-bind

11
推荐指数
1
解决办法
1362
查看次数

我可以将:: bind()提升为Objective C函数吗?

我不知道这是否可行,但如果是,那么语法是什么样的?

如果不可能,为什么不呢?

objective-c boost-bind

10
推荐指数
1
解决办法
1950
查看次数

如何声明extern"C"函数指针

所以我有这个代码:

#include "boost_bind.h"
#include <math.h>
#include <vector>
#include <algorithm>

double foo(double num, double (*func)(double)) {
  return 65.4;
}

int main(int argc, char** argv) {
  std::vector<double> vec;
  vec.push_back(5.0);
  vec.push_back(6.0);
  std::transform(vec.begin(), vec.end(), vec.begin(), boost::bind(foo, _1, log));
}
Run Code Online (Sandbox Code Playgroud)

并收到此错误:

        return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);
.............................................................^
%CXX-E-INCOMPATIBLEPRM, argument of type "double (* __ptr64 )(double) C" is
          incompatible with parameter of type "double (* __ptr64 )(double)"
          detected during:
            instantiation of ...5 pages of boost
Run Code Online (Sandbox Code Playgroud)

所以这个错误是因为'log'在math.h中是外部的"C"

我想知道如何在foo()中声明我的函数指针参数,以便它处理extern"C"函数.

c++ function-pointers boost-bind

10
推荐指数
2
解决办法
1万
查看次数

如何将boost :: bind与不可复制的params一起使用,例如boost :: promise?

有些C++对象没有复制构造函数,但有移动构造函数.例如,boost :: promise.如何使用移动构造函数绑定这些对象?

#include <boost/thread.hpp>

void fullfil_1(boost::promise<int>& prom, int x)
{
  prom.set_value(x);
}

boost::function<void()> get_functor() 
{
  // boost::promise is not copyable, but movable
  boost::promise<int> pi;

  // compilation error
  boost::function<void()> f_set_one = boost::bind(&fullfil_1, pi, 1);

  // compilation error as well
  boost::function<void()> f_set_one = boost::bind(&fullfil_1, std::move(pi), 1);

  // PS. I know, it is possible to bind a pointer to the object instead of 
  // the object  itself. But it is weird solution, in this case I will have
  // to take cake …
Run Code Online (Sandbox Code Playgroud)

c++ boost-bind boost-thread boost-function

10
推荐指数
2
解决办法
2654
查看次数

asio :: placeholder :: error有什么用?

asio库在很多例子中都传递了一个错误参数,即; http://think-async.com/Asio/asio-1.5.3/src/examples/echo/async_tcp_echo_server.cpp

这个参数有什么意义?asio实际上是否填充了此参数的错误?

如果我从处理程序函数中删除它,它编译得很好.

c++ boost boost-bind boost-asio

10
推荐指数
1
解决办法
4482
查看次数

关于shared_ptr和指向成员运算符` - >*`和`std :: bind`的指针

最近我发现shared_ptr没有指向成员运算符的指针->*.我创建了简单的例子:

template <typename Pointer, typename Function, typename... Args>
auto invoke1(Pointer p, Function f, Args... args) -> decltype((p->*f)(args...))
{
  return (p->*f)(args...);
}
struct A { 
    void g() { std::cout << "A::g()\n"; } 
};
int main() {
  A a;
  invoke1(&a, &A::g); // works!!
  std::shared_ptr<A> sa = std::make_shared<A>();
  invoke1(sa, &A::g); // compile error!!
}
Run Code Online (Sandbox Code Playgroud)

Q1:为什么会这样?为什么shared_ptr没有这个运算符?

我添加了这样的运算符,shared_ptr示例开始工作:

template <typename T, typename Result>
auto operator ->* (std::shared_ptr<T> pointer, Result (T::*function)()) ->decltype(std::bind(function, pointer))
{
    return std::bind(function, pointer);
}
template <typename …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading boost-bind shared-ptr pointer-to-member

10
推荐指数
1
解决办法
1999
查看次数

使用for_each和boost :: bind与指针向量

我有一个指针向量.我想为每个元素调用一个函数,但该函数需要引用.是否有一种简单的方法来取消引用元素?

例:

MyClass::ReferenceFn( Element & e ) { ... }

MyClass::PointerFn( Element * e ) { ... }

MyClass::Function()
{
    std::vector< Element * > elements;
    // add some elements...

    // This works, as the argument is a pointer type
    std::for_each( elements.begin(), elements.end(),
                   boost::bind( &MyClass::PointerFn, boost::ref(*this), _1 ) );

    // This fails (compiler error), as the argument is a reference type
    std::for_each( elements.begin(), elements.end(),
                   boost::bind( &MyClass::ReferenceFn, boost::ref(*this), _1 ) );
}
Run Code Online (Sandbox Code Playgroud)

我可以创建一个带指针的脏小包装器,但我认为必须有更好的方法吗?

c++ boost boost-bind

9
推荐指数
1
解决办法
6057
查看次数

如何强制转换为boost :: bind(&myClass :: fun,this,_1,_2,_3)到typedef void(*fun)(arg1,arg2,arg3)?

在lib Bullet中定义了一种类型:

typedef void (*btNearCallback)(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, const btDispatcherInfo& dispatchInfo);
Run Code Online (Sandbox Code Playgroud)

在那里的文档中提供了一个使用示例(第23页):

void MyNearCallback(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, const btDispatcherInfo& dispatchInfo) {
        // Do your collision logic here
        // Only dispatch the Bullet collision information if you want the physics to continue
        dispatcher.defaultNearCallback(collisionPair, dispatcher, dispatchInfo);
}
Run Code Online (Sandbox Code Playgroud)

我将这个示例代码复制到我的类defention中,所以我的类得到了这个函数,我将能够做这样的演员:

    dispatcher->setNearCallback(boost::bind(&BulletAPIWrapper::MyNearCallback, this, _1, _2, _3));
Run Code Online (Sandbox Code Playgroud)

而不是像dispatcher->setNearCallback(MyNearCallback);Bullet教程那样的C.

然而我的VS2010 sp1给了我一个错误:

Error   44  error C2664: 'btCollisionDispatcher::setNearCallback' : cannot convert parameter 1 from 'boost::_bi::bind_t<R,F,L>' to 'btNearCallback'
Run Code Online (Sandbox Code Playgroud)

所以我想知道如何将boost :: bind转换为这样的typedef?

是否有可能具有静态类函数(或至少是全局函数):

void MyNearCallback(btBroadphasePair& collisionPair, btCollisionDispatcher& …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-bind c++03

9
推荐指数
1
解决办法
1175
查看次数

如何在复合类型上使用Boost.Bind?

我有std::map<int, std::pair<short, float> >,我需要short在这张地图中找到最小值.我该怎么boost::bindstd::min_element()

boost::lambda

c++ boost-bind boost-lambda

8
推荐指数
2
解决办法
992
查看次数

类成员函数使用boost :: bind和boost :: function作为回调函数

我正在设置一个成员函数作为我正在使用的C库的回调.C库设置如下的回调:

typedef int (*functionPointer_t)(myType1_t*, myType2_t*, myType3_t*);

setCallback(param1, param2, functionPointer, param4)
Run Code Online (Sandbox Code Playgroud)

我想使用boost :: bind(如果可能的话)传入函数指针.我希望指向的函数是实例化类的成员,而不是静态成员.例如

Class A {
 public: 
  A();
 protected:
  int myCallback(myType1_t*, myType2_t*, myType3_t*); //aka functionPointer_t
}
Run Code Online (Sandbox Code Playgroud)

可以使用boost :: bind和boost :: function来完成吗?Per 如何将类成员函数作为回调传递?(第3个答案)似乎我可以声明以下内容(某处或作为typedef):

boost::function<int (A*, myType1_t*, myType2_t*, myType3*> myCallbackFunction
Run Code Online (Sandbox Code Playgroud)

然后在A(ctor)的某处调用boost :: bind在该类型上,并将其传递给C库调用.

这可能吗,还是我不在基地?非常感谢.

c++ boost boost-bind boost-function

7
推荐指数
1
解决办法
1万
查看次数