|
Boost : |
From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2003-12-07 08:55:48
"Jonathan Turkanis" <technews_at_[hidden]> wrote in message
news:bquivn$ufp$1_at_sea.gmane.org...
> I skipped the thread about discovering array sizes until tonight, when I
> discovered I need the size of an array type as a compile-time constant --
> without partial specialization. Reading through the discussion, I didn't
> find a proprosed implementation.
>
> Is this simple implementation correct?
>
> template<typename T>
> struct array_size {
> static const T* t;
> BOOST_STATIC_CONSTANT(size_t, value = sizeof(T) / sizeof(*t[0]));
> };
>
> For good measure, one could add
> BOOST_STATIC_ASSERT(boost::is_array<T>::value).
your implementation looks ok (I have not testet it), but the problem is
often that you want the size of a variable and not a
type, eg,
sizer( my_array_variable );
which makes the code look a lot different.
best regards
Thorsten
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk