Boost logo

Boost :

From: Kühl, Dietmar (Dietmar.Kuehl_at_[hidden])
Date: 1998-12-02 07:06:00


> This one went by on comp.std.c++ as a way to get an integral
> constant for an array size as sizeof(size(a)):
> template<typename T,const size_t N>
> char const (&sizer(T (&)[N]))[N] {}
> Which inspired:
> template<typename T,const size_t N>
> T* begin(T(&a)[N]) { return a; }
> template<typename T,const size_t N>
> T* end(T(&a)[N]) { return a+N; }
> template<typename T,const size_t N>
> size_t size(T(&)[N]) { return N; }
> And you'd want free forms of these for STL containers as well.
Actually, the three functions were the starting point! I posted them
in summer 1997 (I think). I have an article ready describing their
advantage which was submitted to the C++ Report but not
accepted (it was just a short one for a special series of articles;
I have not came around to make a full blown article out of it yet).

The major problem is that the 'size()' function cannot be used to
produce an integral constant expression, althouth the compiler
can in principle determine the size as an integral expression.
Thus, to really get a constant integral expression suitable eg.
to determine the size of an array, you apparently need the
'sizer()' function together with 'sizeof()'. However, the 'size()'
function worked at least with gcc (not using the extension for
dynamically sized arrays).

Anyway, I think I have a collection of free forms for the STL
containers floating around somewhere. I just have to find them,
document them, and can then submit them.

Free Web-based e-mail groups --

Boost list run by bdawes at, gregod at, cpdaniel at, john at