Re: [boost] Geometry/Vector Lib

-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Martin Bonner Sent: Friday, September 22, 2006 6:52 AM To: boost@lists.boost.org Subject: Re: [boost] Geometry/Vector Lib
----Original Message---- From: Sohail Somani
Marcus Lindblom wrote:
I've sometimes done just:
class vector3d { public: float x, y, z;
float operator[]( int index ) const { return (&x)[index]; }
/* ... */ }; Won't that not work in the presence of padding?
In theory, yes. In practice, I can't see why any compiler would bother adding such padding.
Theory is what holds the C++ world together! I wouldn't want to knowingly invoke UB unless I can prove its worth my while (for example, in the case of hardware specific optimizations). Sohail

Sohail Somani wrote:
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Martin Bonner Sent: Friday, September 22, 2006 6:52 AM To: boost@lists.boost.org Subject: Re: [boost] Geometry/Vector Lib
----Original Message---- From: Sohail Somani
Marcus Lindblom wrote:
I've sometimes done just:
class vector3d { public: float x, y, z;
float operator[]( int index ) const { return (&x)[index]; }
/* ... */ }; Won't that not work in the presence of padding? In theory, yes. In practice, I can't see why any compiler would bother adding such padding.
Theory is what holds the C++ world together! I wouldn't want to knowingly invoke UB unless I can prove its worth my while (for example, in the case of hardware specific optimizations).
You can add pretty easily some tests to see if it holds true for the compiler you're running on. But small vector libs is such a matter of taste and convenience, just like coding style. No solution will please everyone. /Marcus

On Fri, Sep 22, 2006 at 10:06:35PM +0200, Marcus Lindblom wrote:
Sohail Somani wrote:
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Martin Bonner Sent: Friday, September 22, 2006 6:52 AM To: boost@lists.boost.org Subject: Re: [boost] Geometry/Vector Lib
----Original Message---- From: Sohail Somani
Marcus Lindblom wrote:
I've sometimes done just:
class vector3d { public: float x, y, z;
float operator[]( int index ) const { return (&x)[index]; }
/* ... */ }; Won't that not work in the presence of padding? In theory, yes. In practice, I can't see why any compiler would bother adding such padding.
Theory is what holds the C++ world together! I wouldn't want to knowingly invoke UB unless I can prove its worth my while (for example, in the case of hardware specific optimizations).
You can add pretty easily some tests to see if it holds true for the compiler you're running on.
Or you can just encode the test into an assertion into the class, as in the code I posted. Especially if you can make it a static assert (which I'm not sure of). It isn't theory if the code only compiles if it works.
But small vector libs is such a matter of taste and convenience, just like coding style. No solution will please everyone.
Geoffrey
participants (3)
-
Geoffrey Irving
-
Marcus Lindblom
-
Sohail Somani