sol::variadic_args work fine with a single usertype constructor, but when using multiple other overloads, it doesn't find a correct constructor when trying to create a new instance. It would be nice if it would find the constructor that uses sol::variadic_args as last resort.
sol::state lua;
struct vec2 { float x, y; };
lua.new_simple_usertype<vec2>("vec2",
sol::call_constructor, sol::factories([](){
return vec2{};
}, [](vec2 const& v) {
return vec2{v};
}, [](sol::variadic_args va) {
vec2 res{};
if (va.size() == 1) {
res.x = va[0].get<float>();
res.y = va[0].get<float>();
} else if (va.size() == 2) {
res.x = va[0].get<float>();
res.y = va[1].get<float>();
} else {
throw sol::error("invalid args");
}
return res;
})
);
lua.script("local v0 = vec2(); local v1 = vec2(1); local v2 = vec2(1, 2); local v3 = vec2(v2)");
What is the error your face when working with this, exactly? Is it selecting the wrong overload?
edit: error message:
[string "local v0 = vec2(); local v1 = vec2(1); local ..."]:1: sol: no matching function call takes this number of arguments and the specified types
stack traceback:
[C]: in function 'vec2'
[string "local v0 = vec2(); local v1 = vec2(1); local ..."]:1: in main chunk
It is not selecting any overload for v1 and v2, because it doesn't seem to find the overload with sol::variadic_args.
Thanks for reporting this: I found out the error and fixed it.
The next commit for sol2 is gonna be huge...