PDA

View Full Version : Vector's misplaced const attribute



Zenol
06-16-2013, 10:41 AM
Thre was a little mistake with the cast operator of vectors :
Type *const means that the pointer is const, and so, doesn't means anything because the user get a copy of the pointer, and can do what he wants with it.
Type const * means that the type is const, and that the user can't modify it (that's what is expected to be returned by a const function).

So, the patch. The second patch remove the cast of this pointer. Ok, it should works because the class do not inheritate from any others, and the class Vector is probably in the set of "stanard_layout" classes. But, because casting this is never a good idea, it's safer to :
1) Explicitly tell that the code rely on the order of fields
2) Return a ref to the first field.

There are of course many other solution, but I think it's the closest to the clan's philosophy. Feel free to do not apply the second patch.

Edit : I also add the "half_float_vector" patch.

rombust
06-16-2013, 10:26 PM
Instead, I suggest that we remove all of:


const Type &operator[](unsigned int i) const { return ((Type *) this)[i]; }
Type &operator[](unsigned int i) { return ((Type *) this)[i]; }
operator Type *() { return (Type *) this; }
operator Type * const() const { return (Type * const) this; }

(for various reasons, too tired to mention now)

Zenol
06-17-2013, 08:35 AM
I'm ok with removing them if it's ok for all clanLib's developers.

One more const patch (same thing).