b2Vec2 operators in C++

I recently incorporated toLua++ into my code and noticed that the +, - and * operators for b2Vec2 are not declared as members of the struct.

This sucks because tolua++ does not support the binding of non member operator binding.

I tried to replace them with equivalent operators as members of the struct, and this compiles fine but causes asserts when I run the code. Plus, I wanted to avoid modifying the box2d source.

I guess this isn't much of a problem since I can live with it (I can just write some metamethods in lua to fix this) but I was just wondering what significance there is in declaring these three operators in this way. Are class member operators incapable of being inlined?
You will run into problems trying to implement float * b2Vec2

Perhaps you got assertions because you could not actually replicate the operators.
It is generally better style to prefer non-friend free functions to methods. See Scott Meyers book Effective C++.

Technically, it improves encapsulation as there are less functions with access to the internals of the class.

Another more immediate advantage is it improves the utility of the class. When operators are free functions either side of the operator can be implicitly converted. When it is a member only the right hand side can be.

BTW, the book I mentioned is really worth your time and money. Scott Meyers is a great author (it doesn't read like a text book). Reading it WILL improve the quality of your code.
-nick
Thanks for the recommendation!

Would unary operators (like negation) make any sense declared in this way?
I've never really considered it. Just shooting from the hip here.

It would improve encapsulation for the same reasons as binary operators.

However, I can't think of a case where it would improve the utility of the operator through conversion. Possibly if the the value it was converting from didn't have matching unary operator.

-nick
It's also nice to have all your operators in one place, having decided that the binary ones are free.
BorisTheBrave wrote:
It's also nice to have all your operators in one place, having decided that the binary ones are free.

how do you mean?
I just mean, it's more consistent to make all operators free operators than half one way and half the other at whim. Of course, some have to be members, so it doesn't really add up to much either way.

Original thread: https://box2d.org/forum/viewtopic.php?t=3480