我一直认为函数指针不需要&符号:
然而,我所看到的每个使用的例子都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第一个参数基本上是函数指针,我错了吗?
我不知道这是否可行,但如果是,那么语法是什么样的?
如果不可能,为什么不呢?
所以我有这个代码:
#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++对象没有复制构造函数,但有移动构造函数.例如,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) asio库在很多例子中都传递了一个错误参数,即; http://think-async.com/Asio/asio-1.5.3/src/examples/echo/async_tcp_echo_server.cpp
这个参数有什么意义?asio实际上是否填充了此参数的错误?
如果我从处理程序函数中删除它,它编译得很好.
最近我发现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
我有一个指针向量.我想为每个元素调用一个函数,但该函数需要引用.是否有一种简单的方法来取消引用元素?
例:
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)
我可以创建一个带指针的脏小包装器,但我认为必须有更好的方法吗?
在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) 我有std::map<int, std::pair<short, float> >,我需要short在这张地图中找到最小值.我该怎么boost::bind用std::min_element()?
boost::lambda?
我正在设置一个成员函数作为我正在使用的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库调用.
这可能吗,还是我不在基地?非常感谢.
boost-bind ×10
c++ ×9
boost ×4
boost-asio ×1
boost-lambda ×1
boost-thread ×1
c++03 ×1
objective-c ×1
shared-ptr ×1