我试图从绑定了Luabind的方法返回一个std::shared_ptr,但它似乎无法识别该类型。
Luabind代码:
module(lua)
[
class_<Character, BaseEntity, std::shared_ptr<Character> > ("Character"),
def("createCharacter", &Character::createCharacter)
];createCharacter代码:
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));
}如果我在Lua脚本中调用此方法,则不会返回任何内容,执行也就到此为止。但是,如果我将该方法更改为返回一个字符*,它将按预期工作。谷歌一下告诉我,返回一个shared_ptr应该不是问题。
我做错了什么?
另外,我有这个代码,这样Luabind就可以理解std::shared_ptr了:
namespace luabind
{
template <typename T>
T* get_pointer(std::shared_ptr<T> const& p)
{
return p.get();
}
}发布于 2013-03-05 01:50:04
我必须解决同样的问题。
这有点复杂。基本上,您需要使用原型定义一个函数
template <typename T>
T* get_pointer(std::shared_ptr<T> const&);此外,此函数还必须与std::shared_ptr驻留在相同的名称空间中,因此是std::。请注意,luabind全局名称空间或 luabind 名称空间中的函数不会工作在中,因为使用special tricks来确保在检查特定类型是否为智能指针时只使用ADL。避免这种情况的唯一方法是在luabind::detail::has_get_pointer_名称空间中定义函数,而不仅仅是在luabind中定义。
但是仅在这个名称空间中定义函数也不会起作用(至少对于Boost <1.53)。尽管从技术上讲,C++标准不允许在名称空间std中定义函数,但这是Boost <1.53的唯一可能方法。然而,从1.53开始,Boost为std::shared_ptr (和std::unique_ptr)定义了自己的boost::get_pointer()重载。对于这个版本,使Boost的get_pointer()在luabind::detail::has_get_pointer_名称空间中可见就足够了,因为在使用智能指针的任何地方,luabind都是using这个函数(参见luabind/get_pointer.hpp头文件)。在std中定义函数甚至不起作用,因为luabind会引起一个不明确的调用。
因此,如果你想要一个适用于Boost <1.53和>= 1.53以及MSVC10(可能还有9)的get_pointer()重载(它们在std::tr1而不是std中定义shared_ptr ),我可以提供我的(历史上增长的;-)头文件:
#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
#endifhttps://stackoverflow.com/questions/15037955
复制相似问题