Incomplete array type

O

Old Wolf

g++ accepts this code:

struct S;
struct T {};

struct Foo
{
static S a[];
static T b[];
};

int main() {}

However, the following are all rejected:

namespace Bar
{
static S a[];
static T b[];
}

static S a[];
static T b[];

What's the rationale for accepting an incomplete array type as a
static struct member (and even an incomplete array of
incomplete types), but not at namespace scope?

Is this ever useful and/or should it ever actually be used?

Are there any other scenarios in C++ where an incomplete array
type can occur?
 
I

Ian Collins

Old said:
g++ accepts this code:

struct S;
struct T {};

struct Foo
{
static S a[];
static T b[];
};

int main() {}

However, the following are all rejected:

namespace Bar
{
static S a[];
static T b[];
}

static S a[];
static T b[];

What's the rationale for accepting an incomplete array type as a
static struct member (and even an incomplete array of
incomplete types), but not at namespace scope?

The former is only a declaration, the static members still have to be
defined somewhere else. The latter to are definitions, which can't be
incomplete.
 
S

sg

Am 12.04.2014 03:48, schrieb Old Wolf:
g++ accepts this code:

struct S;
struct T {};

struct Foo
{
static S a[];
static T b[];
};

int main() {}

However, the following are all rejected:

namespace Bar
{
static S a[];
static T b[];
}

static S a[];
static T b[];

What's the rationale for accepting an incomplete array type as a
static struct member (and even an incomplete array of
incomplete types), but not at namespace scope?

See what Ian Collins said.

If you want to turn your namespace-scope definitions into declarations
you need the extern keyword for that:

extern S my_static_array1[];
extern T my_static_array2[];
Are there any other scenarios in C++ where an incomplete array
type can occur?

AFAIK only for declarations of arrays with static storage.

The (possibly incomplete) array type is also sometimes used for function
parameters but the compiler immediately replaces this with a pointer type.

Cheers!
sg
 

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,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top