
On Sun, 25 Jun 2006 13:11:32 -0400, me22 <me22.ca@gmail.com> wrote:
On 6/25/06, Gennaro Prota <gennaro_prota@yahoo.com> wrote:
Yes, of course. I've reattached an implementation I proposed here a couple of years ago. The reason why I'm asking is basically that it would require some "somersault" to make it work with VC6 and other broken compilers, so I woldn't spend time on that if there is no interest.
The attachment didn't come out properly for me.
Here's it, copy-and-pasted: // -- array_counter.hpp -- // // (C) Gennaro Prota 2004 // // Use, modification, and distribution is subject to the Boost Software // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy // at http://www.boost.org/LICENSE_1_0.txt) // // ------------------------------------------- #ifndef MAYBE_BOOST_ARRAY_COUNTER_HPP_GP_20040130 #define MAYBE_BOOST_ARRAY_COUNTER_HPP_GP_20040130 #include <cstddef> namespace maybe_boost { template <std::size_t, typename> struct count_dim; template <std::size_t dim, typename T, std::size_t n> struct count_dim<dim, T[n]> { static const std::size_t value = count_dim<dim-1, T>::value; }; template <typename T, std::size_t n> struct count_dim<0, T[n]> { static const std::size_t value = n; }; template <std::size_t dim, typename T, std::size_t n> char (&array_count(T(&)[n])) [count_dim<dim, T[n]>::value]; } #endif // include guard
I think it's worth including. It's not all that uncommon; even Freenode/##C++'s channel bot has the following factoid: Don't use sizeof() to get the size of an array, because sizeof() will do the wrong thing if that 'array' is actually a pointer. Use the following instead: template <typename T, size_t N> size_t array_size(T (&)[N]) { return N; }
That one has the advantage that it can be used without sizeof, but can't be used in constant expressions. My version is exactly the opposite in terms of pros/cons. Of course one can define a macro: #define BOOST_ARRAY_COUNT(a) ( sizeof array_count<0>(a) ) #define BOOST_ARRAY_COUNT_DIM(a, d) ...
Of course that one only works for the outermost dimension of the array and I have no idea whether it works in VC6 at all.
Oh, it doesn't :) Not even if you limit to one dimension. Even in that case it can't understand T(&)[N] and deduce T and N from that. But I'm not sure we should support VC6 more than with a sizeof/sizeof "fallback" version. BTW, I would like to make this work for std::tr1::array as well. --Gennaro.