Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Automatically compute array size

17 views
Skip to first unread message

mathieu

unread,
Jun 4, 2013, 2:50:08 AM6/4/13
to
How could I simplify the following code:

$ cat test.cxx
static const int array[] = {1, 2, 3, 4};

struct S {
const char *name;
const int (&values)[4]; // why '4' explicit required ?
};

static const S s = {
"foobar",
array
};

int main(int, char *[])
{
return 0;
}

I'd like to avoid doing the counting myself. I'd rather let the preprocessor do it for me (C99 ?). Or maybe a little template<> indirection could help ?

Thanks for info,
-M

Ian Collins

unread,
Jun 4, 2013, 3:35:01 AM6/4/13
to
As long as the compiler can see the declaration of array, you can use
the old C trick:

const int (&values)[sizeof(array)/sizeof(*array)];

--
Ian Collins

Francis Glassborow

unread,
Jun 4, 2013, 4:00:52 AM6/4/13
to
I suspect that is not actually what the OP was hoping for. The problem
is that within the definition of S the compiler needs actual types. In
this simple example there is only one instance of S and so the C trick
will work. However, it ceases to be useful if we want more than one
instance of S with different size arrays.

The answer is that such cannot be done without templates (or some
equivalent from C++11) because each size array is a different type and
so references to them are also different types. At the point of
definition of the struct the members must be identifiable types. A
template gets round this by creating a family of types with the same
basic name refined by the actual types in use.

struct S {
const int (&values)[2];
};


struct S {
const int (&values)[3];
};

struct S {
const int (&values)[4];
};

struct S {
const int (&values)[5];
};


Are all different types that can only share a name via templates where
the names might be refined to:

S<2>, S<3>, S<4>, S<5>

I sometimes describe a class template as providing the 'family name' for
a group of loosely related (by behaviour) types that will need the
addition of a 'personal name'.

The problem with the OP's original code is that at the point of
definition of S the compiler has no way to know which member of the
family is being defined. By the time the instance s is defined it is too
late.

Francis

>

mathieu

unread,
Jun 4, 2013, 6:38:31 AM6/4/13
to
Obviously ! that make sense now. I was confused and thought for a minute that const int (&values)[] could be a type.

Thanks,
0 new messages