sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Function Arguments Break With sol::optional<sol::table>

Open atom0s opened this issue 4 months ago • 0 comments

sol version: https://github.com/ThePhD/sol2/commit/4de99c5b41b64b7e654bf8e48b177e8414a756b7 Lua version: MoonJit via https://github.com/AshitaXI/moonjit/commit/094d971c0a9f76025e2c61fab65969aea7fdb753 Compiler version: VS2022 17.13.6 (Using C++23 /std:c++latest x86)

This is also tested against x86-64 gcc (trunk) on Godbolt.

Issue Description:

There appears to be a bug with sol::optional that will break additional arguments when using multiple optionals, if one of the optionals is a sol::optional<sol::table>. When this is used, any additional following argument that is optional will no longer process properly if that given argument is nil.

For example:

auto lua_test5(
    sol::optional<sol::table> arg1,
    sol::optional<int32_t> arg2) -> void
{
    std::printf(std::format("test5: {} - {}\n", arg1.has_value() ? "Y" : "N", arg2.value_or(0)).c_str());
}

Calling this function has unexpected results, such as:

test5({}, 1234);    -- should print: Y - 1234
test5(nil, 1234);   -- should print: N - 1234
test5(nil, nil);    -- should print: N - 0

Instead, this will print:

test5: Y - 1234
test5: N - 0
test5: N - 0

Here is a Godbolt link showing the issue happening along with some additional testing to see if the issue happened with other optional usage. It seems to only trigger with tables. https://godbolt.org/z/nY936vecK

atom0s avatar Aug 13 '25 22:08 atom0s