Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-02-02 09:33:36


Gennaro Prota <gennaro_prota_at_[hidden]> writes:

> On Sat, 31 Jan 2004 09:12:58 -0700, "Jonathan Turkanis"
> <technews_at_[hidden]> 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

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk