473,394 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

containers or array types gubbins

Hello,

It is not possible to assign values to, say, a vector<float[3]> due to the
fact that arrays are not assignable.
Theoretically there's a simple workaround which involves wrapping the array
in a struct, e.g.

struct float3
{
float m[3];
};

....and then using vector<float3>
The problem with this approach is, that there's no guarantee that
sizeof(float[3]) == sizeof(float3).

My question boils down to: is there any non-contrived generic way to treat
containers of array types in an analogous way to non-array types?

Cheers,
b
--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #1
2 1287
bartek wrote:
It is not possible to assign values to, say, a vector<float[3]> due to the
fact that arrays are not assignable.
I think it's actually stricter than that. You just cannot have
vector<float[3]>, period.
Theoretically there's a simple workaround which involves wrapping the array
in a struct, e.g.

struct float3
{
float m[3];
};

...and then using vector<float3>
Why "theoretically"? It's a work-around. Work-arounds are always
practical, not theoretical.
The problem with this approach is, that there's no guarantee that
sizeof(float[3]) == sizeof(float3).
Why do you think it's a problem? sizeof(float3::m) == sizeof(float[3])
What doesn't the difference in sizes between the struct and its member
let you achieve? Are you concerned with the fact that an array of, say,
10 float3 will not be the same as 10*3*sizeof(float)? Is that _really_
so important? Why? Do you need to read an array (vector's storage) of
those structs in one huge 'read' operation? Can you not split it into
several smaller ones?

My question boils down to: is there any non-contrived generic way to treat
containers of array types in an analogous way to non-array types?


Well, where standard containers are concerned, there are no "containers
of array types". If you devise your own container (that doesn't have to
satisfy container requirements put forth by the Standard), you can have
whatever you want, and it's probably not going to be "contrived" (whatever
you mean by that).

Victor
Jul 22 '05 #2
Victor Bazarov <v.********@comAcast.net> wrote in
news:V3***************@newsread1.dllstx09.us.to.ve rio.net:
bartek wrote:
It is not possible to assign values to, say, a vector<float[3]> due
to the fact that arrays are not assignable.


I think it's actually stricter than that. You just cannot have
vector<float[3]>, period.
Theoretically there's a simple workaround which involves wrapping the
array in a struct, e.g.

struct float3
{
float m[3];
};

...and then using vector<float3>


Why "theoretically"? It's a work-around. Work-arounds are always
practical, not theoretical.


Theoretically for me. Some workarounds are more practical than others.
See below.
The problem with this approach is, that there's no guarantee that
sizeof(float[3]) == sizeof(float3).


Why do you think it's a problem? sizeof(float3::m) ==
sizeof(float[3]) What doesn't the difference in sizes between the
struct and its member let you achieve? Are you concerned with the
fact that an array of, say, 10 float3 will not be the same as
10*3*sizeof(float)? Is that _really_ so important? Why? Do you need
to read an array (vector's storage) of those structs in one huge
'read' operation? Can you not split it into several smaller ones?


I'm dealing with a C API which uses pointers to arrays of arrays all
around its interfaces.
Those arrays are usually quite big. Therefore I want to keep them in my
C++ code in a binary compatible fashion, so I can get rid of wasteful
copying on the API boundary, and just pass a &*data.begin().

My question boils down to: is there any non-contrived generic way to
treat containers of array types in an analogous way to non-array
types?


Well, where standard containers are concerned, there are no
"containers of array types". If you devise your own container (that
doesn't have to satisfy container requirements put forth by the
Standard), you can have whatever you want, and it's probably not going
to be "contrived" (whatever you mean by that).


Thanks. It seems throwing together a custom container is the way to go.

Cheers.

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rowan | last post by:
Greetings: I need some guidance. I have been using UDT's and arrays to hold information so I can easily display data and save it. I am in the process of building a new form and ran across a...
11
by: Juicer_X | last post by:
Hello, I have some code that I'm working on, the problem isn't that it doesn't work it's that it's too slow. I have a class that holds my homemade class within an std::map, within an std::map....
6
by: Ares Lagae | last post by:
Hello, I am trying to create a container the stl way, and I have a couple of questions. The code of the container in question can be found at ...
14
by: phil_gg04 | last post by:
Dear C++ Experts, Over the last couple of months I have been writing my first program using shared memory. It has been something of an "in-at-the-deep-end" experience, to say the least. At...
7
by: BigMan | last post by:
Which is the preferred way to create a const container in general: 1. container_type< value_type > const; or 2. container_type< value_type const >?
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
9
by: moleskyca1 | last post by:
I've ran into a problem with inheritance and STL containers. The code is too much to list, but here is the basic problem: #include <queue> using namespace std; class Base { public:
11
by: jimxoch | last post by:
Hi list, Most STL containers are storing their data on the heap. (although some std::string implementations are notable exceptions) Of course, using the heap as storage increases flexibility and...
3
by: massysett | last post by:
Greetings, Having classes with member objects that have STL containers of objects whose definitions are incomplete results in undefined behavior. See for example: ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.