From: Richard Howard on
> Try
> vector< array<float, 3> >
>
> With array being from TR1 or boost.
>

I did this, but how do I then pass it to a function with a signature
like ...

virtual void GetPointAll(float iCoordinates[][3]) const;

my standard practice would be to use something like ...

std::vector<boost::array<float, 3> > coords(numPoints);
sourceOfPoints->GetPointAll(&flts[0]);

but this obviously gives me a boost::array<float, 3> * how do I get
the float[][3]? Am I forced into a reinterpret cast? And will that
work?

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

From: Juan Pedro Bolivar Puente on
On 02/03/10 15:26, Francis Glassborow wrote:
> Mathias Gaunard wrote:
>> On 1 mar, 21:26, Richard Howard wrote:
>> > I would be inclined to do something like this:
>> > struct Point3D
>> > {
>> > float value[3];
>> > float operator[](int i) const { return value[i]; }
>> > float& operator[](int i) { return value[i]; }
>> > };
>> > vector<Point3D> coords;
>> > coords.resize(sourceOfPoints->size());
>> > sourceOfPoints->GetPointAll(coords);
>>> But I believe the compiler is free to add padding to the struct,
>>
>> Not in this case.
>
> Why not?

While definitely it could add it and be ok with the standard, it would
be strange to find an architecture+compiler where it doesnt exist k�N
such that sizeof(float) = k * /machine_word_size/... and no sensible
compiler would add padding in that case, would it? Still, we can make
soure we detect such a problem statically with a static_assert<sizeof
(Point3d) == 3*sizeof(float)>; or one can still try to avoid the padding
with compiler intrinsics in almost any sensible compiler... (yes, not
standard compliant code still).

JP

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

From: Mathias Gaunard on
On Mar 2, 1:26 pm, Francis Glassborow
<francis.glassbo...(a)btinternet.com> wrote:
> Mathias Gaunard wrote:
> > On 1 mar, 21:26, Richard Howard wrote:
> > > I would be inclined to do something like this:
> > > struct Point3D
> > > {
> > > float value[3];
> > > float operator[](int i) const { return value[i]; }
> > > float& operator[](int i) { return value[i]; }
> > > };
> > > vector<Point3D> coords;
> > > coords.resize(sourceOfPoints->size());
> > > sourceOfPoints->GetPointAll(coords);
> >> But I believe the compiler is free to add padding to the struct,
>
> > Not in this case.
>
> Why not?

I am not a standardese expert as many in this group are, but from
reading the standard I came to the conclusion that a POD structure
with a single member has the same layout as that member type.
Note this is important to guarantee that array<T, N> has the same
layout as T[N].


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

From: Mathias Gaunard on
On Mar 2, 7:29 pm, Richard Howard <rhphotography....(a)googlemail.com>
wrote:
> > Try
> > vector< array<float, 3> >
>
> > With array being from TR1 or boost.
>
> I did this, but how do I then pass it to a function with a signature
> like ...
>
> virtual void GetPointAll(float iCoordinates[][3]) const;
>
> my standard practice would be to use something like ...
>
> std::vector<boost::array<float, 3> > coords(numPoints);
> sourceOfPoints->GetPointAll(&flts[0]);
>
> but this obviously gives me a boost::array<float, 3> * how do I get
> the float[][3]? Am I forced into a reinterpret cast? And will that
> work?

It will work, yes. I'm afraid there is no other solution I can think
of.


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