Topic: Implementing countof(array)


Author: fluxsmith@my-dejanews.com
Date: 1998/10/07
Raw View
I have code using the following macro:

#define countof(x) (sizeof(x) / sizeof((x)[0]))

Given macros are evil <g>, in particular I would like 'countof' scoped to my
namespace, the following seemed to me to be a plausible replacement:

template<typename T, size_t n>
inline size_t countof(T(&a)[n])
{
   return sizeof(a) / sizeof(a[0]);
}

The above won't compile, what is a correct non-macro implementation for
countof?

Thanks.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/10/08
Raw View
fluxsmith@my-dejanews.com wrote:
>
> Given macros are evil <g>, in particular I would like 'countof' scoped
> to my namespace, the following seemed to me to be a plausible
> replacement:
>
> template<typename T, size_t n>
> inline size_t countof(T(&a)[n])
> {
>    return sizeof(a) / sizeof(a[0]);
> }

Just return n. However, be aware that even that doesn't work on all
compilers. Borland 5.01 barfs on it, for reasons I can't fathom. But the
compiler's definitely wrong.

--

Ciao,
Paul
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: devphil@my-dejanews.com
Date: 1998/10/08
Raw View
In article <6ve88l$6en$1@nnrp1.dejanews.com>,
  fluxsmith@my-dejanews.com wrote:

> I have code using the following macro:

> #define countof(x) (sizeof(x) / sizeof((x)[0]))

> Given macros are evil <g>, in particular I would like 'countof' scoped to my
> namespace, the following seemed to me to be a plausible replacement:

> template<typename T, size_t n>
> inline size_t countof(T(&a)[n])
> {
>    return sizeof(a) / sizeof(a[0]);
> }

> The above won't compile, what is a correct non-macro implementation for
> countof?

Dietmar Kuehl and Paul DeRocco came up with some good ones in March 1997
("Iterators for built-in arrays" is the thread subject).  Given the function
above, the code body you want, I believe, is

     return n;

Luck++;
/dev/phil

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Jonathan H Lundquist" <fluxsmith@fluxsmith.com>
Date: 1998/10/08
Raw View
Unfortunately, MSVC 6.0 also barfs on it.

Paul D. DeRocco wrote in message <361AFDE8.225709EE@ix.netcom.com>...

>fluxsmith@my-dejanews.com wrote:

>> template<typename T, size_t n>
>> inline size_t countof(T(&a)[n])
>> {
>>    return sizeof(a) / sizeof(a[0]);
>> }

>Just return n. However, be aware that even that doesn't work on all
>compilers. Borland 5.01 barfs on it, for reasons I can't fathom. But the
>compiler's definitely wrong.




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/10/08
Raw View
fluxsmith  <fluxsmith@my-dejanews.com> writes:

> template<typename T, size_t n> inline size_t countof(T(&a)[n])
> { return sizeof(a) / sizeof(a[0]); }

> The above won't compile, what is a correct non-macro implementation for
> countof?

The one above is correct, except that size_t should be std::size_t,
and you must #include <cstdlib> before you can refer to size_t.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:oliva@gnu.org mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: deepblack@geocities.com (Luis Coelho)
Date: 1998/10/09
Raw View
On 07 Oct 98 03:40:20 GMT, fluxsmith@my-dejanews.com uttered the
following words:

>Given macros are evil <g>, in particular I would like 'countof' scoped to my
>namespace, the following seemed to me to be a plausible replacement:
>
>template<typename T, size_t n>
>inline size_t countof(T(&a)[n])
>{
>   return sizeof(a) / sizeof(a[0]);
>}
>

What about

template<class T, size_t N>
inline size_t countof(T (&a)[N])
{
 return N;
}

?

It seems to work with g++, but I've never seen anyone propose
this solution anywhere. It's always been sizeof(a)/sizeof(a[0]).

Regards,
Lums Coelho.

*******
Translations: Any combination between German, English & Portuguese.
Contact me at deepblack@geocities.com
*******
C++ Programming Language, 3rd Ed. by B. Stroustrup. My exercise answers at:
http://www.geocities.com/SiliconValley/Way/3972/index.html


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/10/09
Raw View
devphil@my-dejanews.com wrote:
>
> Dietmar Kuehl and Paul DeRocco came up with some good ones in March
> 1997 ("Iterators for built-in arrays" is the thread subject).

In all honesty, it wasn't me. Thanks anyway.

--

Ciao,
Paul


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]