compile-time array count

Having a look to some old list messages I've seen there has been some discussion about discovering the number of elements of an array in compile-time. Sorry if I have simply misunderstood something here (I'm rapidly catching up with old threads), but isn't this enough? http://article.gmane.org/gmane.comp.lib.boost.devel/3216 That's for expressions, of course, not type-ids. Genny.

"Gennaro Prota" <gennaro_prota@yahoo.com> wrote
Having a look to some old list messages I've seen there has been some discussion about discovering the number of elements of an array in compile-time. Sorry if I have simply misunderstood something here (I'm rapidly catching up with old threads), but isn't this enough?
Similar technique is used in container_traits library (written by Thorsten Ottosen). Look for size_type.hpp, in sandbox or Files section. Use: n = sizeof(sizer(an_array)) /Pavel

On Wed, 28 Jan 2004 18:40:28 +0100, "Pavel Vozenilek" <pavel_vozenilek@hotmail.com> wrote:
"Gennaro Prota" <gennaro_prota@yahoo.com> wrote
Having a look to some old list messages I've seen there has been some discussion about discovering the number of elements of an array in compile-time. Sorry if I have simply misunderstood something here (I'm rapidly catching up with old threads), but isn't this enough?
Similar technique is used in container_traits library (written by Thorsten Ottosen).
Look for size_type.hpp, in sandbox or Files section. Use: n = sizeof(sizer(an_array))
I can't look at it right now, anyway I was just pointing out that it is possible (and easy), in case no one did that. Just one note: since this is a very elementary facility that concerns built-in arrays only I would prefer it to be indipendent from the container_traits library, if it will ever get into boost. Genny.

"Gennaro Prota" <gennaro_prota@yahoo.com> wrote in message:
"Gennaro Prota" <gennaro_prota@yahoo.com> wrote
Similar technique is used in container_traits library (written by Thorsten Ottosen).
Look for size_type.hpp, in sandbox or Files section. Use: n = sizeof(sizer(an_array))
I can't look at it right now, anyway I was just pointing out that it is possible (and easy), in case no one did that.
The current array sizing facility is not standard-conforming. I assume Thorsten is going to fix it, but it doesn't look like he has yet. Jonathan

On Wed, 28 Jan 2004 16:44:42 -0700, "Jonathan Turkanis" <technews@kangaroologic.com> wrote:
The current array sizing facility is not standard-conforming. I assume Thorsten is going to fix it, but it doesn't look like he has yet.
A conforming implementation is really two lines of code. It's fun to write, but admittedly of limited usefulness, in that I would think in properly designed code the needed number of elements would be already exposed through a named constant. So, the only cases where I can imagine this to be useful is either when you need the count of the array to play some unorthodox trick, or when you have third-party code that "forgot" to expose the right constant. Genny. begin 644 array_counter.hpp M+R\@+2T@87)R87E?8V]U;G1E<BYH<'`@+2T-"B\O#0HO+R`H0RD@1V5N;F%R M;R!0<F]T82`R,#`T#0HO+PT*+R\@(%5S92P@;6]D:69I8V%T:6]N+"!A;F0@ M9&ES=')I8G5T:6]N(&ES('-U8FIE8W0@=&\@=&AE($)O;W-T(%-O9G1W87)E M#0HO+R`@3&EC96YS92P@5F5R<VEO;B`Q+C`N("A3964@86-C;VUP86YY:6YG M(&9I;&4@3$E#14Y315\Q7S`N='AT(&]R(&-O<'D-"B\O("!A="!H='1P.B\O M=W=W+F)O;W-T+F]R9R],24-%3E-%7S%?,"YT>'0I#0HO+PT*+R\@("TM+2TM M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2T-"@T*(VEF M;F1E9B!-05E"15]"3T]35%]!4E)!65]#3U5.5$527TA04%]'4%\R,#`T,#$S M,`T*(V1E9FEN92!-05E"15]"3T]35%]!4E)!65]#3U5.5$527TA04%]'4%\R M,#`T,#$S,`T*#0HC:6YC;'5D92`\8W-T9&1E9CX-"@T*#0IN86UE<W!A8V4@ M;6%Y8F5?8F]O<W0@>PT*#0H@("`@=&5M<&QA=&4@/'-T9#HZ<VEZ95]T+"!T M>7!E;F%M93X-"B`@("!S=')U8W0@8V]U;G1?9&EM.PT*#0H@("`@=&5M<&QA M=&4@/'-T9#HZ<VEZ95]T(&1I;2P@='EP96YA;64@5"P@<W1D.CIS:7IE7W0@ M;CX-"B`@("!S=')U8W0@8V]U;G1?9&EM/&1I;2P@5%MN73X-"B`@("U8W0@8V]U M;G1?9&EM/#`L(%1;;ET^#0H@("`@>PT*("`@("`@("!S=&%T:6,@8V]N<W0@ M<W1D.CIS:7IE7W0@=F%L=64@/2!N.PT*("`@('T[#0H-"@T*("`@('1E;7!L M871E(#QS=&0Z.G-I>F5?="!D:6TL('1Y<&5N86UE(%0L('-T9#HZ<VEZ95]T M(&X^#0H@("`@8VAA<B`H)F%R<F%Y7V-O=6YT*%0H)BE;;ETI*2!;8V]U;G1? M9&EM/&1I;2P@5%MN73XZ.G9A;'5E73L-"@T*?0T*#0HC96YD:68@+R\@:6YC ,;'5D92!G=6%R9`T* ` end

"Gennaro Prota" <gennaro_prota@yahoo.com> wrote in message news:umnl1055i9m8bq3gft7um0ercu1179nl22@4ax.com...
On Wed, 28 Jan 2004 16:44:42 -0700, "Jonathan Turkanis" <technews@kangaroologic.com> wrote:
The current array sizing facility is not standard-conforming. I assume Thorsten is going to fix it, but it doesn't look like he has yet.
A conforming implementation is really two lines of code. It's fun to write, but admittedly of limited usefulness, in that I would think in properly designed code the needed number of elements would be already exposed through a named constant. So, the only cases where I can imagine this to be useful is either when you need the count of the array to play some unorthodox trick, or when you have third-party code that "forgot" to expose the right constant.
Well, I can't really argue with that. But it seemes that a conforming implementation would be better than a non-conforming one. Jonathan

On Sat, 31 Jan 2004 09:12:58 -0700, "Jonathan Turkanis" <technews@kangaroologic.com> wrote:
Well, I can't really argue with that. But it seemes that a conforming implementation would be better than a non-conforming one.
Oh yes, I didn't imply that a non-conforming implementation is more lines or more useful :) Just that this is really a small facility; I like it, but more because I'm a C++ fan than because of its actual applications (we must admit that sometimes :)) That said, I would like to see it into boost, but only in its own header, without odd medleys with, say, container_traits or other extensive libraries. Genny.

On Sat, 31 Jan 2004 09:12:58 -0700, "Jonathan Turkanis" <technews@kangaroologic.com> wrote:
Well, I can't really argue with that.
Maybe that's up to me :) There are really cases where you want the compiler to count the elements for you (initialization with the [] = {...} syntax) and retrieve that number *later*. Incredibly, the first C++ file I opened this morning showed me this unfairly neglected situation. And it was my code... Please don't comment on that :) unsigned long numbers[] = { 0, 1, 40247 }; const std::size_t array_count = sizeof(numbers) / sizeof(numbers[0]); for (std::size_t i = 0; i < array_count; ++i) { ... } Note the loop over the array elements. The whole code is in a unit test and each number in the array initializer list corresponds to a test case of a function. So adding/removing numbers means adding/removing test cases. As the code is, I can remove any number from the list without having to modify anything else. Instead, if you write const std::size_t n = 3; unsigned long numbers[n] = { 0, 1, 40247 }; and then drop one of the numbers without also fixing the definition of n you change semantics (the additional element(s) are initialized to zero) usually with no compiler warning. Basically this is a situation where you have a list of objects and want to use them all, no matter how many they are; the fact that you have to count them is an implementation detail due to the lack of a for-each construct in the language. PS: Of course I'm not saying one has to use a template for this; I guess most people would consider the sizeof/sizeof technique safe enough (chances that you use a pointer instead of an array are quite low). But, anyway... :) Genny.

Gennaro Prota <gennaro_prota@yahoo.com> writes:
On Sat, 31 Jan 2004 09:12:58 -0700, "Jonathan Turkanis" <technews@kangaroologic.com> wrote:
Well, I can't really argue with that.
Maybe that's up to me :) There are really cases where you want the compiler to count the elements for you (initialization with the [] = {...} syntax) and retrieve that number *later*.
Yep, this is the key use-case.
PS: Of course I'm not saying one has to use a template for this; I guess most people would consider the sizeof/sizeof technique safe enough (chances that you use a pointer instead of an array are quite low). But, anyway... :)
There is still potential for problems: void f(int x[25]) { for (int i = 0; i < sizeof(x)/sizeof(*x); ++i) { ... } } Don't forget how often the code of experts is copy/pasted by novices ;-) -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (4)
-
David Abrahams
-
Gennaro Prota
-
Jonathan Turkanis
-
Pavel Vozenilek