sol2
sol2 copied to clipboard
sol::main_protected_function uses wrong function pointer when calls lambda when compiled with GCC
Following code compiled with GCC and Clang produces different output:
#include <sol/state.hpp>
#include <sol/object.hpp>
#include <sol/forward.hpp>
#include <cstdio>
int main() {
sol::state state;
auto f0 = [] { std::printf("f0\n"); };
auto f1 = [] { std::printf("f1\n"); };
sol::main_protected_function f0_object = sol::make_object(state, f0);
sol::main_protected_function f1_object = sol::make_object(state, f1);
f1_object();
return 0;
}
Clang built program produces correct output:
f1
GCC is wrong:
f0
https://godbolt.org/z/dKG4rbdoe
Running this under debugger shows that here fx has value of &decltype(f0)::operator() instead of &decltype(f1)::operator().
The problem is in sol::usertype_traits::metatable function. It produces the same result for both lambda types because it relies on __PRETTY_FUNCTION__ therefore function pointer for the second one is not registered. Clang doesn't have this problem because __PRETTY_FUNCTION__ produces different results for different lambdas: https://godbolt.org/z/PxGrhMh7a.