Rapptz / sol

A C++11 Lua wrapper
MIT License
209 stars 32 forks source link

Operator overloading #42

Closed notlion closed 10 years ago

notlion commented 10 years ago

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 :smile:

Rapptz commented 10 years ago

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

ThePhD commented 10 years ago

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

Rapptz commented 10 years ago

I have tried something like this, without luck

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

notlion commented 10 years ago

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.

Rapptz commented 10 years ago

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

notlion commented 10 years ago

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 );
}
ThePhD commented 10 years ago

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.

notlion commented 10 years ago

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.

ThePhD commented 10 years ago

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.

ThePhD commented 10 years ago

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.