tac*_*ice 4 c++ lua shared-ptr luabind
我试图从与 Luabind 绑定的方法返回一个 std::shared_ptr,但它似乎无法识别类型。
Luabind 代码:
module(lua)
[
class_<Character, BaseEntity, std::shared_ptr<Character> > ("Character"),
def("createCharacter", &Character::createCharacter)
];
Run Code Online (Sandbox Code Playgroud)
创建字符代码:
std::shared_ptr<Character> Character::createCharacter(Game* gameInstance, const Character::CharacterSetup& characterSetup, string sprite, b2World* world)
{
return std::shared_ptr<Character>(new Character(gameInstance, characterSetup, sprite, world));
}
Run Code Online (Sandbox Code Playgroud)
如果我在 Lua 脚本中调用此方法,则不会返回任何内容,并在那里停止执行。但是,如果我更改方法以返回 Character*,它会按预期工作。一些谷歌搜索告诉我返回一个 shared_ptr 应该不是问题。
我究竟做错了什么?
另外,我有这个代码,所以 Luabind 可以理解 std::shared_ptr:
namespace luabind
{
template <typename T>
T* get_pointer(std::shared_ptr<T> const& p)
{
return p.get();
}
}
Run Code Online (Sandbox Code Playgroud)
我必须解决同样的问题。
这有点复杂。基本上你需要用原型定义一个函数
template <typename T>
T* get_pointer(std::shared_ptr<T> const&);
Run Code Online (Sandbox Code Playgroud)
该函数还必须与 驻留在同一命名空间中std::shared_ptr,因此std::。请注意,全局命名空间或luabind命名空间(如您的命名空间)中的函数将无法工作,因为 luabind 使用特殊技巧来确保在检查某种类型是否为智能指针时仅使用 ADL。避免这种情况的唯一方法是在命名空间中定义函数,luabind::detail::has_get_pointer_而不仅仅是在luabind.
但是单独在这个命名空间中定义函数也不起作用(至少对于 Boost <1.53)。std尽管C++ 标准在技术上不允许在命名空间中定义函数,但这是 Boost <1.53 的唯一可能的方法。然而,从 1.53 开始,Boost 为( 和)定义了自己的boost::get_pointer()重载。对于这个版本来说,让 Boost在命名空间中可见就足够了,因为 luabind在任何使用智能指针的地方都是这个函数(参见标题)。那么定义函数甚至不起作用,因为 luabind 会引发不明确的调用。std::shared_ptrstd::unique_ptrget_pointer()luabind::detail::has_get_pointer_usingluabind/get_pointer.hppstd
因此,如果您想要适用于 Boost <1.53 和 >= 1.53 以及 MSVC 10(也许是 9)(这些定义在而不是)get_pointer()的重载,我可以为您提供我的(历史上增长的 ;-) )标头:std::shared_ptrshared_ptrstd::tr1std
#ifndef SHAREDPTR_CONVERTER_HPP_INCLUDED
#define SHAREDPTR_CONVERTER_HPP_INCLUDED SHAREDPTR_CONVERTER_HPP_INCLUDED
#include <boost/version.hpp>
#if BOOST_VERSION >= 105300
#include <boost/get_pointer.hpp>
namespace luabind { namespace detail { namespace has_get_pointer_ {
template<class T>
T * get_pointer(std::shared_ptr<T> const& p) { return p.get(); }
}}}
#else // if BOOST_VERSION < 105300
#include <memory>
// Not standard conforming: add function to ::std(::tr1)
namespace std {
#if defined(_MSC_VER) && _MSC_VER < 1700
namespace tr1 {
#endif
template<class T>
T * get_pointer(shared_ptr<T> const& p) { return p.get(); }
#if defined(_MSC_VER) && _MSC_VER < 1700
} // namespace tr1
#endif
} // namespace std
#endif // if BOOST_VERSION < 105300
#endif
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1248 次 |
| 最近记录: |