Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't bind union members to usertype #1409

Closed
Lifaon opened this issue Sep 25, 2022 · 0 comments · Fixed by #1603
Closed

Can't bind union members to usertype #1409

Lifaon opened this issue Sep 25, 2022 · 0 comments · Fixed by #1603

Comments

@Lifaon
Copy link

Lifaon commented Sep 25, 2022

Greetings,

First of all, I am using these versions, installed via vcpkg:

  • Lua version 5.4.4
  • sol2 version 3.3.0

I seem to have trouble assigning union members to usertypes.
The compiler produces an error, suggesting that I might be trying to bind members to an unrelated class.
Are "raw" unions simply not a thing in Lua/sol2? I'm sorry if it's stated somewhere, I couldn't find any information about it.

Here is the error message:

It seems like you might have accidentally bound a class type with a member function method that does not correspond to the class. For example, there could be a small type in your new_usertype(...) binding, where you specify one class "T" but then bind member methods from a complete unrelated class. Check things over!

Here is a simple main.cpp to reproduce the issue:

#include <sol/sol.hpp>

struct S {
    int i;
    char c[4];
};

union U {
    int i;
    char c[4];
};

int main()
{
    sol::state lua;

    // Test with struct
    sol::usertype<S> struct_type = lua.new_usertype<S>("S");
    struct_type["i"] = &S::i;
    struct_type["c"] = &S::c;

    // Test with union
    sol::usertype<U> union_type = lua.new_usertype<U>("U");
    union_type["i"] = &U::i; // <--- Compiler error
    union_type["c"] = &U::c; // Never reached
}

Same happens trying directly from the new_usertype() function:

lua.new_usertype<U>("U",
    "i", &U::i,
    "c", &U::c
); // Compiler error

Thanks in advance for the help.

@Lifaon Lifaon changed the title Can't assign union members to usertype Can't bind union members to usertype Sep 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant