Incomplete array types (with unspecified size) ?

T

Timothy Madden

Hello

Why does C++ not allow incomplete array types as C does ?

Like this:

struct ArchiveHeader
{
size_t iSize;
int iVerMaj;
int iVerMin;
size_t nDataLen;
unsigned char
data[]; // unspecified size
};
// ArchiveHeader remains an incomplete type,
// can only be used in a pointer or reference type
 
J

James Kanze

Why does C++ not allow incomplete array types as C does ?
Like this:
struct ArchiveHeader
{
size_t iSize;
int iVerMaj;
int iVerMin;
size_t nDataLen;
unsigned char
data[]; // unspecified size
};
// ArchiveHeader remains an incomplete type,
// can only be used in a pointer or reference type

Because C didn't have this "feature" when C++ was being
designed, and because it causes all sorts of problems when you
start to use such structures.
 
F

Fred Zwarts

Timothy Madden said:
Hello

Why does C++ not allow incomplete array types as C does ?

Like this:

struct ArchiveHeader
{
size_t iSize;
int iVerMaj;
int iVerMin;
size_t nDataLen;
unsigned char
data[]; // unspecified size
};
// ArchiveHeader remains an incomplete type,
// can only be used in a pointer or reference type

In addition to the other replies: Because C++ has std::vector.
 
V

Vladimir Jovic

Timothy said:
Hello

Why does C++ not allow incomplete array types as C does ?

Like this:

struct ArchiveHeader
{
size_t iSize;
int iVerMaj;
int iVerMin;
size_t nDataLen;
unsigned char
data[]; // unspecified size
};
// ArchiveHeader remains an incomplete type,
// can only be used in a pointer or reference type

There was a quite lengthy thread recently about structure hack, and that
seams to be what you are trying to do
 
S

Seungbeom Kim

Because it's a different language.

If it were just a different language, it wouldn't have a design rule
saying "No gratuitous incompatibilities with C." While it is true that
C++ is a different language from C, that alone doesn't justify an
incompatibility. Neither would adopting the feature deprive C++ of its
identity distinct from C.

Your answer gives an impression that the C++ committee decided to remove
or to not add the feature just to be different, which is certainly not true.
As others have said in this thread, there are better reasons to give.
 
J

James Kanze

On 2010-09-21 09:02, Victor Bazarov wrote:
If it were just a different language, it wouldn't have a design rule
saying "No gratuitous incompatibilities with C." While it is true that
C++ is a different language from C, that alone doesn't justify an
incompatibility. Neither would adopting the feature deprive C++ of its
identity distinct from C.

The rule is "as close to C as possible, but no closer".
Your answer gives an impression that the C++ committee decided
to remove or to not add the feature just to be different,
which is certainly not true.

Sort of. I don't think that the C++ committee actually
"decided" anything. C added this feature. To add it to C++
would require a proposal, with a careful study of the impact and
restrictions (e.g. you probably can't derive from such a beast);
in such a proposal, remaining compatible with C would certainly
count as a strong motivation. But as far as I know, no one felt
the issue important enough to make the effort of writing such
a proposal. In other words, the committee counts on the efforts
of volenteer workers, and nobody volenteered.
 
J

Juha Nieminen

Fred Zwarts said:
In addition to the other replies: Because C++ has std::vector.

How many times does this have to be repeated? std::vector is not a
substitute for "Type array[];". It's a substitute for "Type* array;".
Different thing (even though the usage syntax is confusingly similar).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top