C++试图从std :: function获取函数地址

Agu*_*rez 13 c++ pointers function std

我试图从std :: function中找到函数的地址.

第一个解决方案是:

size_t getAddress(std::function<void (void)> function) {
    typedef void (fnType)(void);
    fnType ** fnPointer = function.target<fnType *>();
    return (size_t) *fnPointer;
}
Run Code Online (Sandbox Code Playgroud)

但这只适用于带有(void())签名的函数,因为我需要签名为(void(Type&))的函数,我试图做

template<typename T>
size_t getAddress(std::function<void (T &)> function) {
    typedef void (fnType)(T &);
    fnType ** fnPointer = function.target<fnType *>();
    return (size_t) *fnPointer;
}
Run Code Online (Sandbox Code Playgroud)

我得到"错误 - 预期"('用于函数式转换或类型构造"

更新:有没有办法捕获成员类地址?对于我正在使用的班级成员:

template<typename Clazz, typename Return, typename ...Arguments>
size_t getMemberAddress(std::function<Return (Clazz::*)(Arguments...)> & executor) {
    typedef Return (Clazz::*fnType)(Arguments...);
    fnType ** fnPointer = executor.template target<fnType *>();
    if (fnPointer != nullptr) {
        return (size_t) * fnPointer;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

更新:捕获lambda我正在使用

template <typename Function>
struct function_traits
    : public function_traits<decltype(&Function::operator())> {
};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const> {
    typedef ReturnType (*pointer)(Args...);
    typedef std::function<ReturnType(Args...)> function;
};

template <typename Function>
typename function_traits<Function>::function
to_function (Function & lambda) {
    return static_cast<typename function_traits<Function>::function>(lambda);
}

template <typename Lambda>
size_t getAddress(Lambda lambda) {
    auto function = new decltype(to_function(lambda))(to_function(lambda));
    void * func = static_cast<void *>(function);
    return (size_t)func;
}

std::cout << getAddress([] { std::cout << "Hello" << std::endl;}) << std::endl;
Run Code Online (Sandbox Code Playgroud)

Rob*_*son 23

template调用目标时需要使用关键字:

#include <functional>
#include <iostream>

template<typename T>
size_t getAddress(std::function<void (T &)> f) {
    typedef void (fnType)(T &);
    fnType ** fnPointer = f.template target<fnType*>();
    return (size_t) *fnPointer;
}

void foo(int& a) {
    a = 0;
}

int main() {
    std::function<void(int&)> f = &foo;
    std::cout << (size_t)&foo << std::endl << getAddress(f) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

提示:当您遇到C++语法问题时,我建议您使用clang++编译代码.如果你如何编写代码的方式,它通常会指向你的写入方向来修复错误(当它可以弄清楚你在做什么时).

我还建议您使用可变参数模板使您的函数更通用:

template<typename T, typename... U>
size_t getAddress(std::function<T(U...)> f) {
    typedef T(fnType)(U...);
    fnType ** fnPointer = f.template target<fnType*>();
    return (size_t) *fnPointer;
}
Run Code Online (Sandbox Code Playgroud)

  • 不,你不能在所有情况下.例如,如果我将函数绑定到带有capture的lambda,`target()`将返回一个空指针,该函数将是segfault.谨慎使用. (5认同)

Hug*_*ins 7

Astd::function只是一个对象,尽管伪装成非对象。因此,我们可以获取此对象的地址,并且这在副本等之间是不变的。

要获得指针,我们需要进行一些转换。例如,给定一个函数f1,我们可以通过执行以下操作来打印隐式指针:

std::cout << "f1: " << *(long *)(char *)&f1 << std::endl;
Run Code Online (Sandbox Code Playgroud)

它的作用是:

  • 取 f1 的地址。f1 本身就是一个指向函数对象的指针,尽管它伪装成一个原始对象
    • 所以这个地址是一个指向函数指针的指针
    • 我们想要的是指向函数的底层指针
  • 将指向指针的指针转换为指向 char 的指针
  • 然后指向指向 long 的指针
  • 现在,假设指针大小为longs,我们可以将此指针取消引用为 long,并获得与函数对象关联的底层地址。

给定两个std::function<void()>sf1f2,我们可以执行以下操作:

std::cout << "f1: " << *(long *)(char *)&f1 << std::endl;
std::cout << "f2: " << *(long *)(char *)&f2 << std::endl;

std::function<void()> f1copy = f1;
std::cout << "\nafter copy f1 into f1copy:" << std::endl;
std::cout << "addresses of f1 and f1copy differ: " << &f1 << " " << &f1copy << std::endl;
std::cout << "but underlying pointers identical: " <<
    *(long *)(char *)&f1 << " " << *(long *)(char *)(&f1copy) << std::endl;

std::cout << "\n after assign f2 to f1copy" << std::endl;
f1copy = f2;
std::cout << "now the underlying pointer of f1copy matches f2's:" << std::endl;
std::cout <<
    *(long *)(char *)&f2 << " " << *(long *)(char *)&f1copy << std::endl;
Run Code Online (Sandbox Code Playgroud)

示例输出:

f1: 4439003784
f2: 4439003912

after copy f1 into f1copy:
addresses of f1 and f1copy differ: 0x7fff572ab970 0x7fff572ab910
but underlying pointers identical: 4439003784 4439003784

 after assign f2 to f1copy
now the underlying pointer of f1copy matches f2's:
4439003912 4439003912
Run Code Online (Sandbox Code Playgroud)

  • 它有效,谢谢!但我必须承认这可能是不安全的,例如,如果编译器使用不同的实现,那么第一个成员可能不是指针,而只是填充或类似的东西(即使我不这样做)认为这可能会发生)。但仍然很好的答案 (2认同)