相关疑难解决方法(0)

比较函数指针

如何比较C++中的函数指针?它稳定吗?

例如,像这样的东西是有效的:

if(pFnc == &myFnc){
//Do something
}
Run Code Online (Sandbox Code Playgroud)

c++

14
推荐指数
1
解决办法
7407
查看次数

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

我试图从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; …
Run Code Online (Sandbox Code Playgroud)

c++ pointers function std

13
推荐指数
2
解决办法
9895
查看次数

理解C函数回调的typedef语法

#include <iostream>

typedef char (*callback)(int *data);
typedef char (callback2)(int *data);
typedef char callback3(int *data);

char fun_a(int *d)
{
    if (*d == 10)
        return 'A';
    else
        return 'B';
}

int main()
{
    int num = 10;
    callback cb_1_a;
    cb_1_a = fun_a;
    std::cout << cb_1_a(&num) << std::endl;

    callback cb_1_b;
    cb_1_b = &fun_a;
    std::cout << cb_1_b(&num) << std::endl;

    callback cb_1_c;
    cb_1_c = &fun_a;
    std::cout << (*cb_1_c)(&num) << std::endl;

    /*
    callback2 cb2;
    cb2 = fun_a;    // wrong

    callback3 cb3;
    cb3 = fun_a;    // wrong …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
2
解决办法
1731
查看次数

比较成员函数的 std::function

我尝试搜索类似的问题:
问题 1
问题 2

但无论如何,我无法比较成员函数。这是一个例子:

class ClassA
{
public:
    int add(int a, int b)
    {
        return a + b;
    }
};

int main()
{
    ClassA a1{};

    function<int(int, int)> f1 = bind(&ClassA::add, a1, placeholders::_1, placeholders::_2);
    function<int(int, int)> f2 = bind(&ClassA::add, a1, placeholders::_1, placeholders::_2);

    cout << boolalpha << "f1 == f2 " << (f1.target_type() == f2.target_type()) << endl; // true
    cout << (f1.target<int(ClassA::*)(int, int)>() == nullptr) << endl; // true

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

从代码中可以明显看出f1f2是不同的。第一个cout显示true …

c++ function-pointers function c++11

3
推荐指数
1
解决办法
1126
查看次数

评估 if ( std::function&lt;void()&gt; == function )?

如何计算给定的表达式(function<void()> == functor)。

代码示例:

#include <functional>
using namespace std;

void Foo() { }

int main() {

    function<void()> a;
    // if (a == Foo) -> error
}
Run Code Online (Sandbox Code Playgroud)

编辑:调试细节,并删除图片。

c++

3
推荐指数
1
解决办法
160
查看次数

标签 统计

c++ ×5

function ×2

c++11 ×1

function-pointers ×1

pointers ×1

std ×1