Emscripten 和从 std::bind 到 std::function 的转换
Emscripten and conversion from std::bind to std::function
我正在尝试使用 emscripten 编译我的项目。
本机在 Visual Studio 2013 年一切正常。
我把函数存储在这里:
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;
}
...
}
以后可以添加简单函数和成员函数:
CBCollection<void, MatrixStack, float> BeforeRenderCBs;
...
AnimatedSprite::AnimatedSprite()
{
using namespace placeholders;
BeforeRenderCBs.addCB(bind(&AnimatedSprite::beforeRenderCB, this, _1, _2));
}
使用 emscripten 结果如下:
<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~19~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~19~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~19~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~19~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)
^
我尝试将 CBFunction 切换为
std::function<Return(Arguments...)
到处都没有解决问题,我需要其他地方的 CBFunction。
emscripten 版本为 1.29.0,clang 3.4
您的类型不匹配。如果查看编译错误,AnimatedSprite::beforeRenderCB
是类型:
void (AnimatedSprite::*)(MatrixStack &, float)
但您正在尝试将其转换为带有签名的 std::function
:
void(MatrixStack, float)
如果您将 collection 类型更改为:
CBCollection<void, MatrixStack&, float> BeforeRenderCBs;
// ^
应该可以。
我正在尝试使用 emscripten 编译我的项目。 本机在 Visual Studio 2013 年一切正常。
我把函数存储在这里:
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;
}
...
}
以后可以添加简单函数和成员函数:
CBCollection<void, MatrixStack, float> BeforeRenderCBs;
...
AnimatedSprite::AnimatedSprite()
{
using namespace placeholders;
BeforeRenderCBs.addCB(bind(&AnimatedSprite::beforeRenderCB, this, _1, _2));
}
使用 emscripten 结果如下:
<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~19~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~19~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~19~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~19~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)
^
我尝试将 CBFunction 切换为
std::function<Return(Arguments...)
到处都没有解决问题,我需要其他地方的 CBFunction。
emscripten 版本为 1.29.0,clang 3.4
您的类型不匹配。如果查看编译错误,AnimatedSprite::beforeRenderCB
是类型:
void (AnimatedSprite::*)(MatrixStack &, float)
但您正在尝试将其转换为带有签名的 std::function
:
void(MatrixStack, float)
如果您将 collection 类型更改为:
CBCollection<void, MatrixStack&, float> BeforeRenderCBs;
// ^
应该可以。