这两者有什么区别吗?还是我的安全,以取代所有出现boost::bind的std::bind在我的代码,从而消除对加速的依赖?
想要将boost :: bind传递给期望普通函数指针(相同签名)的方法.
typedef void TriggerProc_type(Variable*,void*);
void InitVariable(TriggerProc_type *proc);
boost::function<void (Variable*, void*)> triggerProc ...
InitVariable(triggerProc);
error C2664: 'InitVariable' : cannot convert parameter 1 from
'boost::function<Signature>' to 'void (__cdecl *)(type *,void *)'
Run Code Online (Sandbox Code Playgroud)
我可以避免存储boost :: function并直接传递绑定的functor,但后来我得到类似的错误:
error C2664: 'blah(void (__cdecl *)(type *,void *))' : cannot convert parameter
1 from 'boost::_bi::bind_t<R,F,L>' to 'void (__cdecl *)(type *,void *)'
Run Code Online (Sandbox Code Playgroud) 我有这样的事情:
class Bar
{
public:
pair<string,string> one;
std::vector<string> cars;
Bar(string one, string two, string car);
};
class Car
{
public:
string rz;
Bar* owner;
Car(string car, Bar* p);
};
class Foo
{
public:
Foo ( void );
~Foo ( void );
int Count ( const string & one, const string & two) const;
int comparator (const Bar & first, const Bar & second) const;
std::vector<Bar> bars;
};
int Foo::comparator(const Bar & first, const Bar & second) const{
return first.name < …Run Code Online (Sandbox Code Playgroud) 我正在设置一个成员函数作为我正在使用的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库调用.
这可能吗,还是我不在基地?非常感谢.
我有一点麻烦了解如何通过这个以被传递到库的回调函数。该功能需要具有特定的签名。
有问题的库是OCILIB(https://vrogier.github.io/ocilib/doc/html/classocilib_1_1_subscription.html),并尝试将类函数作为Register()的第4个参数传递。
我可以顺利通过以下
&database::callback // a static function of database
Run Code Online (Sandbox Code Playgroud)
要么
[](ocilib::Event &event) // as lambda function
{
}
Run Code Online (Sandbox Code Playgroud)
但它无权访问实例变量。我试图像
[&](ocilib::Event &event) // as lambda function
{
}
Run Code Online (Sandbox Code Playgroud)
但签名不匹配,出现以下错误
database.cpp: In member function ‘bool dcn::database::watch(std::__cxx11::string)’:
database.cpp:104:44: error: no matching function for call to ‘ocilib::Subscription::Register(ocilib::Connection&, std::__cxx11::string&, ocilib::Subscription::ChangeTypesValues, dcn::database::watch(std::__cxx11::string)::<lambda(ocilib::Event&)>, unsigned int, unsigned int)’
}, (unsigned int) 7778, (unsigned int) 0);
^
In file included from /usr/include/ocilib.hpp:9194:0,
from /home/ai/dcn/include/main.h:17,
from database.cpp:1:
/usr/include/ocilib_impl.hpp:6650:13: note: candidate: void ocilib::Subscription::Register(const ocilib::Connection&, const ostring&, …Run Code Online (Sandbox Code Playgroud) I am attempting to add a low level mouse hook to a class. I am able to do so by placing this function in my CPP file:
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
//my hook code here
return CallNextHookEx(0, nCode, wParam, lParam);
}
Run Code Online (Sandbox Code Playgroud)
Then, I set up the hook in the class constructor like so:
HHOOK mousehook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
Run Code Online (Sandbox Code Playgroud)
This works fine for intercepting mouse events, however since the callback function is not defined …
c++ ×6
boost ×3
boost-bind ×2
c++11 ×1
c++builder ×1
const ×1
function ×1
functor ×1
hook ×1
member ×1
non-static ×1
stdbind ×1
winapi ×1
windows ×1