sol2
sol2 copied to clipboard
Reference parameters with std::function from lua.
std::function<bool(const Entity &,const Entity &)> eq { lua["test_equal"] };
Entity a,b;
eq(std::ref(a),std::ref(b));
Will call Entity(const Entity &) and copy-construct parameters to lua function "test_equal".
auto eq { lua["test_equal"] };
Entity a,b;
eq.call<bool>(std::ref(a),std::ref(b));
Will use references as i would expect.
Can i get the std::function way to also use references and not copy stuff?
The issue does not seem to occur in godbolt, which claims to use sol 3.2.1 and Lua 5.3.5. But when using v3.2.2 and Lua 5.3.5, the issue occurs.
The following workaround seems to work though:
std::function<bool(const std::reference_wrapper<Entity>, const std::reference_wrapper<Entity>)> eq { lua["test_equal"] };
Entity a,b;
eq(std::ref(a),std::ref(b));
But its not very pretty.
What version are you using?
commit a7da2a8e889498b0c5a4ed6d9e3463af02bb6102 (HEAD -> develop, origin/develop, origin/HEAD)
ah, hm yes - i did not think of using reference_wrapper. thanks for the info!
This should work normally as expected. I'll dig into it.
The issue does not seem to occur in godbolt, which claims to use sol 3.2.1 and Lua 5.3.5. But when using v3.2.2 and Lua 5.3.5, the issue occurs.
This has been causing me some headaches after a recent upgrade from Sol 3.0.3 to Sol 3.3.0. But I had assumed it was a deliberate change to reflect the fact that Lua is not const-aware, and that any claim to be passing a const-reference to a Lua function is in fact a lie. If it is not intended, that would explain its absence from any release notes!