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
std::size_t a_elem_count = sizeof(a) / sizeof(int);
> Thanks,
> Bogdan
Regards
--
Cholo Lennon
Bs.As.
ARG
#error Number of elements expected is 5
#endif
Vladimir Grigoriev
Vladimir Grigoriev
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
> 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
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" <bog...@company.com> wrote in message
news:%23xK%23esVLJ...@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);
Just a correction due to some break line:
It gives non-obvious compiler message, though.
"Bogdan" <bog...@company.com> wrote in message
news:erbdNyUL...@TK2MSFTNGP02.phx.gbl...
inline void check_size(int (&)[4]) {}
check_size(a);
Will give an error if the size is not 4.
>
> Thanks,
> Bogdan
I made it even shorter:
int (&guarantee_size_is_4)[4] = a;
>
> Thanks,
> Bogdan
This is perfect.
Thanks,
Bogdan
Thanks,
Bogdan
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
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
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...