简体   繁体   English

如何获取 lambda *capture* function 的“原始”ptr

[英]How to get the “raw” ptr of a lambda *capture* function

I want to make a C++ assembly hooking using lambda captures but for this i need to get the pointer of a lambda capture function. I want to make a C++ assembly hooking using lambda captures but for this i need to get the pointer of a lambda capture function.

something like this:像这样的东西:

int val0 = 42;
auto lambdaCap = new auto([&]() -> int { return val0++; });
uint64_t pLambdaFn = static_cast<uint64_t>(&decltype(*lambdaCap)::operator()); // need this

I understand than a lambda function capture seems like an instance of class with a functor, but i want get the static address of lambda::operator(). I understand than a lambda function capture seems like an instance of class with a functor, but i want get the static address of lambda::operator(). In memory "lambdaCap" is just a ptr to the variables members used in the lambda.在 memory 中,“lambdaCap”只是 lambda 中使用的变量成员的一个指针。

Thanks谢谢

&decltype(*lambdaCap)::operator()) is not valid because decltype(*lambdaCap) is actually an lvalue reference to the closure type. &decltype(*lambdaCap)::operator())无效,因为decltype(*lambdaCap)实际上是对闭包类型的左值引用。

Instead, std::remove_pointer_t<decltype(lambdaCap)> would give you the closure type itself.相反, std::remove_pointer_t<decltype(lambdaCap)>会给你闭包类型本身。 So you can write &std::remove_pointer_t<decltype(lambdaCap)>::operator() to get the pointer-to-member-function corresponding to the closure type's function call operator.因此,您可以编写&std::remove_pointer_t<decltype(lambdaCap)>::operator()来获取与闭包类型的 function 调用运算符对应的指向成员函数的指针。

However, this expression has type int (T::*)() , which cannot be converted into an integer type.但是,此表达式的类型为int (T::*)() ,无法转换为 integer 类型。 You can store it as-is, and call it using a pointer to an instance of the closure type (such as lambdaCap itself) but you can't convert it to uint64_t .您可以按原样存储它,并使用指向闭包类型实例的指针(例如lambdaCap本身)调用它,但不能将其转换为uint64_t There is no way to convert a pointer-to-nonstatic-member to an integer type, whether with static_cast or reinterpret_cast or any other cast.没有办法将指向非静态成员的指针转换为 integer 类型,无论是使用static_cast还是reinterpret_cast或任何其他强制转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM