热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Emscripten和从std::bind到std::function的转换

如何解决《Emscripten和从std::bind到std::function的转换》经验,为你挑选了1个好方法。

我正在尝试使用emscripten编译我的项目.在Visual Studio 2013中,一切都很好.

我在这里存储函数:

template
using CBFunction = std::function;

typedef unsigned int CBID;

template
class CBCollection
{
    std::map> cbs;
public:
    CBID addCB(CBFunction cb)
    {
        CBID id = findFreeID();
        cbs[id] = cb;
        return id;
    }

    ...
}

后来我可以添加简单和成员函数:

CBCollection BeforeRenderCBs;
...
AnimatedSprite::AnimatedSprite()
{
    using namespace placeholders;
    BeforeRenderCBs.addCB(bind(&AnimatedSprite::beforeRenderCB, this, _1, _2));
}

与emscripten这是结果:

:624:1: note: expanded from here
"C:/Development/LexUnitEngine/Engine/include/Buffer.h"
^
C:\Development\LexUnitEngine\Engine\source\AnimatedSprite.cpp:76:24: error: no viable conversion from '__bind &, std::__1::placeholders::__ph<2> &>' to 'CBFunction'
      (aka 'std::__1::function')
        BeforeRenderCBs.addCB(bind(&AnimatedSprite::beforeRenderCB, this, _1, _2));
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\PROGRA~1\EMSCRI~1\EMSCRI~1\129~1.0\system\include\libcxx\functional:1448:5: note: candidate constructor not viable: no known conversion from '__bind &, std::__1::placeholders::__ph<2> &>' to
      'nullptr_t' for 1st argument
    function(nullptr_t) _NOEXCEPT : __f_(0) {}
    ^
C:\PROGRA~1\EMSCRI~1\EMSCRI~1\129~1.0\system\include\libcxx\functional:1449:5: note: candidate constructor not viable: no known conversion from '__bind &, std::__1::placeholders::__ph<2> &>' to
      'const std::__1::function &' for 1st argument
    function(const function&);
    ^
C:\PROGRA~1\EMSCRI~1\EMSCRI~1\129~1.0\system\include\libcxx\functional:1450:5: note: candidate constructor not viable: no known conversion from '__bind &, std::__1::placeholders::__ph<2> &>' to
      'std::__1::function &&' for 1st argument
    function(function&&) _NOEXCEPT;
    ^
C:\PROGRA~1\EMSCRI~1\EMSCRI~1\129~1.0\system\include\libcxx\functional:1454:41: note: candidate template ignored: disabled by 'enable_if' [with _Fp = std::__1::__bind &, std::__1::placeholders::__ph<2>
      &>]
                                        __callable<_Fp>::value &&
                                        ^
C:/Development/LexUnitEngine/Engine/include/CallbackCollection.h:49:46: note: passing argument to parameter 'cb' here
        CBID addCB(CBFunction cb)
                                                    ^

我试图将CBFunction切换到

std::function

无处不在,但它没有解决问题,我需要其他地方的CBFunction.

emscripten版本是1.29.0,clang 3.4



1> Barry..:

您的类型不匹配.如果你看一下编译错误,AnimatedSprite::beforeRenderCB是类型:

void (AnimatedSprite::*)(MatrixStack &, float)

但是你试图将它转换为std::function带签名的:

void(MatrixStack, float)

如果您将集合类型更改为:

CBCollection BeforeRenderCBs;
//                            ^

它应该工作.


推荐阅读
author-avatar
tuigone
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有