sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Reference parameters with std::function from lua.

Open ninnghazad opened this issue 3 years ago • 4 comments

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?

ninnghazad avatar Jan 04 '22 17:01 ninnghazad

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?

Rochet2 avatar Jan 07 '22 22:01 Rochet2

commit a7da2a8e889498b0c5a4ed6d9e3463af02bb6102 (HEAD -> develop, origin/develop, origin/HEAD)

ah, hm yes - i did not think of using reference_wrapper. thanks for the info!

ninnghazad avatar Jan 11 '22 21:01 ninnghazad

This should work normally as expected. I'll dig into it.

ThePhD avatar Jan 29 '22 04:01 ThePhD

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!

MJCollett avatar Jul 18 '22 08:07 MJCollett