Sol2: overloaded usertype constructor with sol::variadic_args

Created on 4 Aug 2017  路  4Comments  路  Source: ThePhD/sol2

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)");

Help.Desk Maybe.Bug...?

All 4 comments

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...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

squeek502 picture squeek502  路  4Comments

karenzshea picture karenzshea  路  4Comments

Erwsaym picture Erwsaym  路  4Comments

spin-scenario picture spin-scenario  路  5Comments

morsisko picture morsisko  路  3Comments