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

Any way to verify static array size at compile time?

2 views
Skip to first unread message

Bogdan

unread,
Oct 13, 2008, 11:58:24 AM10/13/08
to
Hi,

Assuming that I have a static array initialized as follows:
int a[] = {1, 2, 3, 4};

Is there any way to verify its size at compile time? That is, I'm looking
for a syntax that is similar to the following that I can do at runtime:
ASSERT(_countof(a) == 4);

Ideally, I'd like to be able to do something similar to what the following
pseudo code does:
#if (_countof(a) != 4)
#error Number of elements expected is 4
#endif

Any suggestions?

Thanks,
Bogdan


Cholo Lennon

unread,
Oct 13, 2008, 12:17:00 PM10/13/08
to

std::size_t a_elem_count = sizeof(a) / sizeof(int);

> Thanks,
> Bogdan

Regards

--
Cholo Lennon
Bs.As.
ARG


Vladimir Grigoriev

unread,
Oct 13, 2008, 12:13:21 PM10/13/08
to

"Bogdan" <bog...@company.com> wrote in message
news:erbdNyUL...@TK2MSFTNGP02.phx.gbl...
const size_t size_of_a = sizeof( a ) / sizeof( a[0] );
#if ( size_of_a != 5 )

#error Number of elements expected is 5

#endif

Vladimir Grigoriev


Vladimir Grigoriev

unread,
Oct 13, 2008, 12:19:45 PM10/13/08
to

"Vladimir Grigoriev" <vlad....@mail.ru> wrote in message
news:O7ZXd6UL...@TK2MSFTNGP04.phx.gbl...

>
> int a[] = {1, 2, 3, 4};
> const size_t size_of_a = sizeof( a ) / sizeof( a[0] );
> #if ( size_of_a != 5 )
>
> #error Number of elements expected is 5
>
> #endif
>
Sorry this does not work at the preprocessor level.

Vladimir Grigoriev


Nathan Mates

unread,
Oct 13, 2008, 12:55:13 PM10/13/08
to
In article <erbdNyUL...@TK2MSFTNGP02.phx.gbl>,

Bogdan <bog...@company.com> wrote:
>Ideally, I'd like to be able to do something similar to what the following
>pseudo code does:
>#if (_countof(a) != 4)
> #error Number of elements expected is 4
>#endif

See http://www.pixelbeat.org/programming/gcc/static_assert.html
[No, I didn't write this, but a coworker pointed it out a while back,
and I've kept the link.]

Nathan Mates
--
<*> Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein

Tamas Demjen

unread,
Oct 13, 2008, 1:12:37 PM10/13/08
to

Bogdan wrote:

> Assuming that I have a static array initialized as follows:
> int a[] = {1, 2, 3, 4};
>
> Is there any way to verify its size at compile time?

Yes, you might find this helpful:
http://www.boost.org/doc/libs/1_36_0/doc/html/boost_staticassert.html

Tom

Bogdan

unread,
Oct 13, 2008, 1:42:09 PM10/13/08
to

"Cholo Lennon" <cholo...@hotmail.com> wrote in message
news:exy2O6UL...@TK2MSFTNGP04.phx.gbl...

>
> std::size_t a_elem_count = sizeof(a) / sizeof(int);
>

Unfortunately this will not help during compile. I'd like to generate
compile error of # of elements in the array is different than expected.

Thanks,
Bogdan


Bogdan

unread,
Oct 13, 2008, 1:56:40 PM10/13/08
to
I meant "... if # of elements..."
Sorry for the typo.

"Bogdan" <bog...@company.com> wrote in message

news:%23xK%23esVLJ...@TK2MSFTNGP04.phx.gbl...

Cholo Lennon

unread,
Oct 13, 2008, 4:22:02 PM10/13/08
to
"Bogdan" <bog...@company.com> wrote in message
news:#EHUT0VL...@TK2MSFTNGP04.phx.gbl...

Ok I misunderstood your point. If you don't want to use boost like others
pointed, you can use a similar solution found in early versions of Loki
library (Thanks to
A.Alexandrescu)(http://sourceforge.net/projects/loki-lib/):


#define STATIC_ASSERT(expr, msg) typedef char
ERROR_STATIC_ASSERTION_##msg[1][(expr)]

void Test()
{


int a[] = { 1 ,2 ,3, 4 };

STATIC_ASSERT(sizeof(a)/sizeof(int) == 5, invalid_array_size);

Cholo Lennon

unread,
Oct 13, 2008, 4:27:01 PM10/13/08
to
"Cholo Lennon" <cholo...@hotmail.com> wrote in message
news:O0KXaFXL...@TK2MSFTNGP05.phx.gbl...


Just a correction due to some break line:

Alexander Grigoriev

unread,
Oct 13, 2008, 10:37:09 PM10/13/08
to
C_ASSERT(_countof(a) == 4);

It gives non-obvious compiler message, though.

"Bogdan" <bog...@company.com> wrote in message

news:erbdNyUL...@TK2MSFTNGP02.phx.gbl...

Ben Voigt [C++ MVP]

unread,
Oct 15, 2008, 12:15:25 PM10/15/08
to

inline void check_size(int (&)[4]) {}

check_size(a);

Will give an error if the size is not 4.

>
> Thanks,
> Bogdan


Ben Voigt [C++ MVP]

unread,
Oct 15, 2008, 12:18:56 PM10/15/08
to

I made it even shorter:

int (&guarantee_size_is_4)[4] = a;

>
> Thanks,
> Bogdan


Bogdan

unread,
Oct 16, 2008, 12:57:51 PM10/16/08
to

"Alexander Grigoriev" <al...@earthlink.net> wrote in message
news:OLSXCXaL...@TK2MSFTNGP03.phx.gbl...

> C_ASSERT(_countof(a) == 4);
>
> It gives non-obvious compiler message, though.
>

This is perfect.

Thanks,
Bogdan


Bogdan

unread,
Oct 16, 2008, 1:06:48 PM10/16/08
to

"Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote in message
news:OlTa6Gu...@TK2MSFTNGP03.phx.gbl...

> I made it even shorter:
>
> int (&guarantee_size_is_4)[4] = a;
>
This is so cool because of its simplicity. I love it - even the compiler
message is right on.
Still, I think I'll stay with C_ASSERT() from winnt.h as suggested by
Alexander so not to confuse other guys who might have to maintain the code.

Thanks,
Bogdan


Ben Voigt [C++ MVP]

unread,
Oct 16, 2008, 1:40:06 PM10/16/08
to

If that's the motivation I would probably consider a comment.

/* We can successfully bind the reference to array only if the length
matches */

>
> Thanks,
> Bogdan


Hendrik Schober

unread,
Oct 21, 2008, 2:26:54 PM10/21/08
to

That'd would confuse me more than Ben's solution. But then I
have been writing portable code for a decade and practically
never touch any <win*.h> header directly...

> Thanks,
> Bogdan

Schobi

Bogdan

unread,
Oct 22, 2008, 1:20:24 PM10/22/08
to
Hendrik,

Point well taken. As I stated in my previous reply, I love Ben's solution
but...
The code that I work with is full of run-time ASSERT() macros. I think that
adding a compile-time C_ASSERT() macro would simply make it more consistent
(this sounds like splitting hair, I know).

In regards to writing portable code... I think that you are quite lucky
that you have been able to write a portable code for the last decade. I
can't see myself doing that while writing DirectShow filters (COM
interfaces) and Direct3D engines. The code is riddled (and has to be so it
does not break with the next release of VS/SDK) with structures and macros
from various platform SDK header files.

Thanks,
Bogdan

"Hendrik Schober" <Spam...@gmx.de> wrote in message
news:u8ZmiC7M...@TK2MSFTNGP04.phx.gbl...

0 new messages