Rapptz / sol

A C++11 Lua wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Operator overloading

notlion opened this issue · comments

Operator overloading has been mentioned a couple times in the issues, but I can't find an example. Is it possible to add an operator overload to a userdata? I have tried something like this, without luck:

sol::userdata<Vec2f> udata("Vec2", ctor, "__add", &Vec2f::operator+);

I'd be happy to add this to the userdata example if it's possible and someone can show me how 😄

This has been in the backburner for a while on my end. It's not explicitly supported at the moment.

While not explicitly supported, that should work right out of the box (literally, just like that). However, I've realized that people having to memorize Lua's meta-table names could be clunky. I've thought of providing an enumeration for a while to enable people to not have to worry about it so much and it would actually require very little work, if I remember the code correctly. Let me drum up and example in a test file and see what happens.~

I have tried something like this, without luck

Any idea what the errors are? They'd be useful to have.

Sure thing. This is the error I get for the code above:

../deps/sol/sol/stack.hpp:313:30: error: no matching member function for call to 'push'
    pusher<Unqualified<T>>{}.push(L, std::forward<T>(t), std::forward<Args>(args)...);
    ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~

The rest is here.

It seems like the const qualifiers are lost along the way.

Aha! So this actually works when I remove the const-ness from the return type. I suppose this behavior is correct given that Lua has no concept of const.

This is the original definition:

const Vec2<T> operator+( const Vec2<T>& rhs ) const {
  return Vec2<T>( x + rhs.x, y + rhs.y );
}

const is indeed the problem (and cinder is a bit idiotic for returning a const _copy_), but this has highlighted some things that need tweaking and fixing. I'll get on it right away.

By the way, the __add method works, once the const goes away, so like I said earlier this indeed works out of the box, but I'm going to provide a more "user-friendly" overload mechanism.

Thanks for the help. I will work from the other end and submit a PR to Cinder which removes const. It seems counterproductive in this case.

The underlying problem is fixed and your code should work, but this issue should remain open as we fully support operator free functions (and free functions that take their first parameter as the T in userdata<T>) rather than just member function overloads.

But, you'll be all set.

This issue is fully resolved as of PR #47, since we can now do free functions and functors to accomodate for operators which are written outside of the class but who's first parameter is a T.