Calling C++ member function from Luabind causes “No matching overload found”

狂风中的少年 提交于 2019-12-03 21:36:23

This is a copy of a mail I sent to the Luabind mailing list. http://sourceforge.net/mailarchive/message.php?msg_id=27420879

I am not sure if this applies to Windows and DLLs, too, but I had a similar experience with GCC and shared modules on Linux: classes registered with Luabind where only valid within that shared library, but caused segmentation faults if used across shared library boundaries.

The solution was to patch the luabind::type_id class, and compare using typeid(T).name() instead of typeid(T)::operator=. For GCC, the reason why the typeid operator might not work across shared libraries is explained here [1]. In this particular case, I loaded the shared library with Lua's require(), which, unfortunately, does not pass RTLD_GLOBAL to dlopen.

[1] http://gcc.gnu.org/faq.html#dso

The typeid equality problem has appeared in other C++ libraries, e.g. in boost::any [2], with the same fix [3], typeid(T).name() comparison.

[2] https://svn.boost.org/trac/boost/ticket/754
[3] https://svn.boost.org/trac/boost/changeset/56168

Maybe the attached patch helps in the case of DLLs, too.

--- include.orig/luabind/typeid.hpp
+++ include/luabind/typeid.hpp
@@ -6,6 +6,7 @@
 # define LUABIND_TYPEID_081227_HPP

 # include <boost/operators.hpp>
+# include <cstring>
 # include <typeinfo>
 # include <luabind/detail/primitives.hpp>

@@ -33,17 +34,17 @@

     bool operator!=(type_id const& other) const
     {
-        return *id != *other.id;
+        return std::strcmp(id->name(), other.id->name()) != 0;
     }

     bool operator==(type_id const& other) const
     {
-        return *id == *other.id;
+        return std::strcmp(id->name(), other.id->name()) == 0;
     }

     bool operator<(type_id const& other) const
     {
-        return id->before(*other.id);
+        return std::strcmp(id->name(), other.id->name()) < 0;
     }

     char const* name() const
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!