From: jason on
Hey all,

Let's say I have a vector type:

struct vec
{
float data[3];
};

and I want to use it to wrap a plain float[3].

So, I want

float x[3];

vec * v(x);

Or something like that.

Is there a way to do this without some crazy reinterpret_cast stuff?

Thanks,
Jason

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: red floyd on
jason wrote:
> Hey all,
>
> Let's say I have a vector type:
>
> struct vec
> {
> float data[3];
> };
>
> and I want to use it to wrap a plain float[3].
>
> So, I want
>
> float x[3];
>
> vec * v(x);
>
> Or something like that.
>
> Is there a way to do this without some crazy reinterpret_cast stuff?


Sure:

struct vec
{
float data[3];
vec(float (&v)[3])
{
std::copy(v, v+3, data);
}
};

float x[3];

vec my_vec(x);

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Alex Shulgin on
On Apr 5, 10:35 pm, jason <jasonsew...(a)gmail.com> wrote:
> Hey all,
>
> Let's say I have a vector type:
>
> struct vec
> {
> float data[3];
>
> };
>
> and I want to use it to wrap a plain float[3].
>
> So, I want
>
> float x[3];
>
> vec * v(x);
>
> Or something like that.
>
> Is there a way to do this without some crazy reinterpret_cast stuff?

I guess that it's appropriate for `vec' to have value semantics, so
you can use a constructor for this:

struct vec
{
explicit vec(float[] x)
{
data[0] = x[0];
data[1] = x[1];
data[2] = x[2];
}
...
};

float x[3];
vec v(x);

--
Cheers,
Alex

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Marcin.Barczynski on
On Apr 5, 11:35 am, jason <jasonsew...(a)gmail.com> wrote:
> Let's say I have a vector type:
>
> struct vec
> {
> float data[3];
>
> };
>
> and I want to use it to wrap a plain float[3].
> So, I want
>
> float x[3];
> vec * v(x);
>
> Or something like that.
> Is there a way to do this without some crazy reinterpret_cast stuff?

Why not simply create a constructor vec(float *data)?

Marcin.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: jason on
On Apr 6, 11:03 am, Marcin.Barczyn...(a)yahoo.com wrote:
> On Apr 5, 11:35 am, jason <jasonsew...(a)gmail.com> wrote:
>
>
>
> > Let's say I have a vector type:
>
> > struct vec
> > {
> > float data[3];
>
> > };
>
> > and I want to use it to wrap a plain float[3].
> > So, I want
>
> > float x[3];
> > vec * v(x);
>
> > Or something like that.
> > Is there a way to do this without some crazy reinterpret_cast stuff?
>
> Why not simply create a constructor vec(float *data)?

{ signature and banner removed -mod }

I guess I wasn't being clear; I don't want to actually copy the data
into the struct (in fact, I had a vec * in my original posting).
Knowing that the types are laid out the same in memory, I'd like a
reasonable way to _treat_ a float[3] as a vec *, assuming that I have
some functions or operators defined on vec.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]