我正在尝试开发线程抽象(来自Windows API的POSIX线程和线程),我非常希望能够使用方法指针启动它们,而不是函数指针.
我想要做的是线程的抽象是一个带有纯虚方法"runThread"的类,它将被植入未来的线程类中.
我还不知道Windows线程,但要启动POSIX线程,您需要一个函数指针,而不是方法指针.而且我无法找到一种方法将方法与实例相关联,因此它可以作为一个函数工作.我可能只是找不到关键字(我一直在搜索很多),我认为它几乎是Boost :: Bind()所做的,所以它必须存在.
你能帮助我吗 ?
c++ methods multithreading member-function-pointers function-pointers
class gfx {
void resize(int x, int y);
}
gfx g;
Run Code Online (Sandbox Code Playgroud)
我能以某种方式将g.resize转换为'void(*)(int,int)'吗?
我想停止警告
server.cpp:823:警告:从'void*(ClientHandler :: )()'转换为'void()(void)'
在通话中:
pthread_create(th, NULL,
(void* (*)(void*)) &ClientHandler::handle,
(void *) clientHandler);
Run Code Online (Sandbox Code Playgroud)
其中handle()是成员函数ClientHandler:
void* ClientHandler::handle();
Run Code Online (Sandbox Code Playgroud)
我很难从编译器中解密函数类型的消息.
问题是:
handle()界面吗?我可以摆脱整体铸造吗?我正在使用libcurl我在类中下载了文件,我希望看到一个进度函数.我注意到我可以设置一个典型的函数指针
curl_easy_setopt(mCurl, CURLOPT_PROGRESSFUNCTION, progress_func3);
Run Code Online (Sandbox Code Playgroud)
但是,我想将它设置为我的类的函数指针.我可以用编译来获取代码
curl_easy_setopt(mCurl, CURLOPT_PROGRESSFUNCTION, &MyClass::progress_func3);
Run Code Online (Sandbox Code Playgroud)
并且progress_func3函数将被调用.问题是,一旦它返回,就会出现"检测到缓冲区溢出!" 错误通过,说程序无法安全地继续执行,必须终止.(这是一个Microsoft Visual C++运行时库错误窗口,我正在使用Visual Studio 2010).
当我使用函数时,没有问题,但是当我使用成员函数指针时,我将收到此错误.如何在libcurl中使用成员函数指针?
给一个班级
class C {
public:
int f (const int& n) const { return 2*n; }
int g (const int& n) const { return 3*n; }
};
Run Code Online (Sandbox Code Playgroud)
我们可以定义一个函数指针p,以C::f这样.
int (C::*p) (const int&) const (&C::f);
Run Code Online (Sandbox Code Playgroud)
定义p可以使用以下方式拆分typedef:
typedef int (C::*Cfp_t) (const int&) const;
Cfp_t p (&C::f);
Run Code Online (Sandbox Code Playgroud)
为了确保p不会改变(p = &C::g;例如),我们可以这样做:
const Cfp_t p (&C::f);
Run Code Online (Sandbox Code Playgroud)
现在,p这种情况下的类型是什么?我们如何在p不使用typedef 的情况下完成最后的定义?我知道typeid (p).name ()无法区分最外面的const,因为它产生了
int (__thiscall C::*)(int const &)const
Run Code Online (Sandbox Code Playgroud) 这是指向接受两个int并返回int的类方法的指针:
template <typename T>
using TFunction = int (T::*)(int, int);
Run Code Online (Sandbox Code Playgroud)
我只能传递非const方法.如何更改此模板以便它接受const和非const方法?
我有一个接口,我必须在其中传递成员函数指针,这些指针被静态转换为基指针,并在我们的背后存储为 void 指针和调用类型(mfc 消息表)。
我创建了一个包装器来执行一些异常处理(非常简单的 try/catch 来捕获 std::runtime_error),因为这很乏味,并且在每个用户回调中都容易出错。
到目前为止,以下包装器实际上运行得相当好(这个想法是直接获取成员函数指针作为值模板参数- 本质上为每个回调函数提供一个成员函数包装器。):
class MyClass : public CWnd/*example*/{
public:
template<void (MyClass::*f)(CCmdUI*)>
void Dispatch(CCmdUI* pCmdUI) {
try {
(this->*f)(pCmdUI);
}
catch (std::runtime_error& e) {
//handle error
}
}
};
Run Code Online (Sandbox Code Playgroud)
但是为了避免对每种类型的调用进行显式重载 - 是否可以对参数列表进行参数化?
插图(注意这不起作用):
template<void (MyClass::*f)(Args...)>
void Dispatch(Args... args) {
Run Code Online (Sandbox Code Playgroud) c++ templates member-function-pointers variadic-templates c++17
我想做这样的事情:
typedef int(A::*f_ptr)(int);
class A
{
int f1(int a){ /*do something*/ }
int f2(int a){ /*do something else*/ }
f_ptr pick_f(int i)
{
if(i)
return this->f1;
return this->f2;
}
}
Run Code Online (Sandbox Code Playgroud)
原因是我希望类 A 的实例保存某些有用的变量,然后根据用户输入只选择我需要的成员函数。但这不起作用,因为我得到“指向绑定函数的指针只能用于调用该函数”。如何编写返回指向非静态成员函数的指针的函数?
我希望能够形成一个只知道类本身和方法名称的成员指针类型。不幸的是,我无法在我的类中使用 const 和非常量方法变体。
示例代码片段:
struct A
{
void method() {}
void method() const {}
};
int main()
{
decltype(&A::method) _;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试了以下方法,但也没有取得多大成功:
decltype(&(std::declval<const A>().method)) _;
Run Code Online (Sandbox Code Playgroud)
decltype由于歧义,这两种方法都失败了,因为无法解决这个问题:
'decltype cannot resolve address of overloaded function'
我怎样才能以其他方式实现这一目标?
让我们考虑一种“调用”函数(这里称为“调用”),它有助于调用由模板参数传递的成员函数。在这个函数中,我需要知道拥有成员函数的类的类型。有没有办法(最好在 c++14 中)这样做?
#include <functional>
template<typename F, typename... Args, std::enable_if_t<std::is_member_pointer<std::decay_t<F>>{}, int> = 0 >
constexpr decltype(auto) call(F&& f, Args&&... args) noexcept(noexcept(std::mem_fn(f)(std::forward<Args>(args)...)))
{
// Here we know that f is a member function, so it is of form : &some_class::some_function
// Is there a way here to infer the type some_class from f ? For exemple to instantiate a variable from it :
// imaginary c++ : class_of(f) var;
return std::mem_fn(f)(std::forward<Args>(args)...);
}
int main()
{
struct Foo { void bar() {} …Run Code Online (Sandbox Code Playgroud)