Marcell Horváth
Marcell Horváth

Reputation: 23

Emscripten and conversion from std::bind to std::function

I'm trying to compile my project with emscripten. Natively in Visual Studio 2013 everything is ok.

I store functions in this:

template<typename Return, typename ...Arguments>
using CBFunction = std::function<Return(Arguments...)>;

typedef unsigned int CBID;

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

    ...
}

Later I can add simple and member functions:

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

with emscripten this is the result:

<scratch space>: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<void (AnimatedSprite::*)(MatrixStack &, float), AnimatedSprite *, std::__1::placeholders::__ph<1> &, std::__1::placeholders::__ph<2> &>' to 'CBFunction<void, MatrixStack, float>'
      (aka 'std::__1::function<void (MatrixStack, float)>')
        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<void (AnimatedSprite::*)(MatrixStack &, float), AnimatedSprite *, std::__1::placeholders::__ph<1> &, 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<void (AnimatedSprite::*)(MatrixStack &, float), AnimatedSprite *, std::__1::placeholders::__ph<1> &, std::__1::placeholders::__ph<2> &>' to
      'const std::__1::function<void (MatrixStack, float)> &' 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<void (AnimatedSprite::*)(MatrixStack &, float), AnimatedSprite *, std::__1::placeholders::__ph<1> &, std::__1::placeholders::__ph<2> &>' to
      'std::__1::function<void (MatrixStack, float)> &&' 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<void (AnimatedSprite::*)(MatrixStack &, float), AnimatedSprite *, std::__1::placeholders::__ph<1> &, 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<Return, Arguments...> cb)
                                                    ^

I tried to switch the CBFunction to

std::function<Return(Arguments...)

everywhere but it didn't solve the problem and I need the CBFunction other places.

emscripten version is 1.29.0, clang 3.4

Upvotes: 2

Views: 654

Answers (1)

Barry
Barry

Reputation: 304152

You have a type mismatch. If you look at the compile error, AnimatedSprite::beforeRenderCB is of type:

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

But you're trying to convert it to a std::function with signature:

void(MatrixStack, float)

If you change your collection type to:

CBCollection<void, MatrixStack&, float> BeforeRenderCBs;
//                            ^

It should work.

Upvotes: 5

Related Questions