执行绑定的std :: function会抛出std :: bad_function_call [英] Executing bound std::function throws std::bad_function_call

查看:639
本文介绍了执行绑定的std :: function会抛出std :: bad_function_call的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将成员函数绑定到std::function<void(void)>.我听说成员函数带有一个额外的参数,它是实例指针.因此,我调用std::bind(&Class::Function, this, parameter),但是当我执行函数对象时,它将引发运行时错误.

I want to bind a member function to a std::function<void(void)>. I heard that member functions take one extra parameter which is the instance pointer. Therefore I call std::bind(&Class::Function, this, parameter) but when I execute the function object, it throws a runtime error.

Application.exe中0x748D4B32处的未处理异常:Microsoft C ++ 例外:内存位置0x0114F4E8的std :: bad_function_call.

Unhandled exception at at 0x748D4B32 in Application.exe: Microsoft C++ exception: std::bad_function_call at memory location 0x0114F4E8.

该参数是指向我自己的struct之一的指针.我怎么做错了?您还需要什么其他信息?

The parameter is a pointer to one of my own structs. How am I doing wrong? What additional information do you need?

更新:这是我的代码.

class ModuleRenderer
{
    struct Pass{ std::function<void()> Function; /* many more members... */ };
    std::vector<std::pair<std::string, Pass>> passes;

    enum Drawfunc{ FORMS, SKY, LIGHTS, QUAD, SCREEN };
    void AddPass(std::string Name, Drawfunc Function)
    {
        Pass pass;
        // set some of the members
        // ...

        passes.push_back(std::make_pair(Name, pass));
        Pass *pointer = &(passes.back().second);

        switch (Function)
        {
        case FORMS:
            pointer->Function = std::bind(&ModuleRenderer::DrawForms, this, pointer);
            break;

            // analogeously for the other cases
            // ...
        }
    }

    void DrawForms(Pass *pass)
    {
        // ...
    }

    // is called consecutively after adding all passes
    void Update()
    {
        for(auto i : passes)
            // some initializing based on members of pass
            i.Function();
    }
};

推荐答案

上面的注释中指出了几个不同的问题.要解决这些问题,请尝试对您的代码进行以下更改:

A couple of different issues have been pointed out in the comments above. To resolve these, try making the following changes to your code:

struct Pass{ std::function<void(Pass *)> Function; /* ... */ };

// ...

case FORMS:
  pointer->Function = 
      std::bind(&ModuleRenderer::DrawForms, this, std::placeholders::_1);
  break;

暂时不要将Pass *绑定到函数调用,因为 @molbdnilo 指出,多次调用AddPass()并调整矢量大小时,指针将变为无效.

Do not bind the Pass * to the function call just yet, because, as @molbdnilo points out, that pointer will become invalid when you call AddPass() multiple times and the vector is resized.

由于std::function现在采用了Pass *,因此在调用它时需要提供正确的指针.

Since the std::function now takes a Pass *, you need to supply the correct pointer when you invoke it.

void Update()
{
    for(auto& i : passes) { // <-- take a reference, don't copy
        // some initializing based on members of pass
        i.Function( &i );   // pass Pass * to the function
}

这篇关于执行绑定的std :: function会抛出std :: bad_function_call的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆