Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2000-08-28 19:03:02


// Determine the number of elements in an array. Works even with broken
// compilers that can't resolve the array size as a function template
// parameter. This is based on a comp.lang.c++.moderated posting by
"Jonathan
// Lundquist" <jhl_at_[hidden]>, who gives the following explanation:
//
// When invoked with a pointer instead of a true array, the template
// returning void will be resolved, which will fail the compile.
//
// When invoked with an array, the template returning int will be
// resolved, so sizeof(a) / sizeof((a)[0]) will be evaluated.
//
// The '0' term of the expression will never be returned, but is there to
// complete the syntax of the ?: expression.
#define BOOST_ARRAY_LENGTH(a) \
(sizeof(boost::detail::not_an_array_type((a), &(a))) ? sizeof(a) /
sizeof((a)[0]) : 0)

namespace boost { namespace detail {
template<typename T>
void not_an_array_type(T* const, T* const*);

template<typename T>
int not_an_array_type(const void*, T);
}}


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