btVector3 operator[] disabled?

User avatar
ejtttje
Posts: 96
Joined: Mon Nov 03, 2008 9:57 pm

btVector3 operator[] disabled?

Post by ejtttje »

It would be convenient to use the operator[] on btVector3, I notice the function is somewhat recently commented out in the btQuadWordStorage... can I get it back? :)
(I have some templated math functions, I could use btVector3's interchangeably with my other math types if they supported operator[])
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: btVector3 operator[] disabled?

Post by Erwin Coumans »

Have you tried using [] on a btVector3? It should automatically use the operator btScalar* operator, which has better performance.

Thanks,
Erwin
User avatar
ejtttje
Posts: 96
Joined: Mon Nov 03, 2008 9:57 pm

Re: btVector3 operator[] disabled?

Post by ejtttje »

It's not a big deal btw... I can also use &v.x()... if you think you might wind up making w the first element for quaternions, I can see why you want to abstract the storage order...
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: btVector3 operator[] disabled?

Post by Erwin Coumans »

The following works just fine, even without the operator[] implementation. As I mentioned before, the btVector::operator btScalar* takes care of that, so no need for &v.x().

Code: Select all

btVector3 pos(1,2,3);
btScalar y = pos[1];
btAssert(y == 2);
Have you tried it?
User avatar
ejtttje
Posts: 96
Joined: Mon Nov 03, 2008 9:57 pm

Re: btVector3 operator[] disabled?

Post by ejtttje »

Erwin Coumans wrote:Have you tried using [] on a btVector3? It should automatically use the operator btScalar* operator, which has better performance.
Yup, this works... should've just tried it instead of checking the documentation first ;)

So WTF! I didn't think that could work like that. The compiler checks all of the implicit conversions to see if it can make the type I'm passing into a type which supports the operator I'm calling? Now that I think about it, I guess I've seen this done with iostream and operator<<, so maybe this shouldn't be so surprising.

Not every day I learn a new C++ trick anymore, thanks :)
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: btVector3 operator[] disabled?

Post by Erwin Coumans »

The implicit conversions can also be very dangerous:

btVector3 a(0,0,0),b(0,0,0);
(a==b) will return false, because it compares the pointers using this same operator btScalar*.

I just added operator != and operator == to make it a bit safer.
Thanks,
Erwin