On Feb 8, 2:21 am, "Alf P. Steinbach" <al...@start.no> wrote:
> * James Kanze:
> > What's wrong with the more or less standard idiom:
> > template< typename T, std::size_t N >
> > std::size_t
> > sizee( T (&array)[ N ] )
> > {
> > return N ;
> > }
> Mainly three things:
> * It doesn't provide a size measure usable at compile time.
> (This is easy to fix by using the return sized-type
> idiom plus a little macro to hide the ugliness).
Good point. I've never needed is as a compile time constant,
but I can imagine that such cases exist.
In C++0x, of course, the function would be declared constexpr,
and could be used in integral constant expressions.
> * In C++98 it doesn't work with array of local type.
> (This is apparently impossible to fix, must wait for C++0x).
This is a problem with templates in general. And this one has
caused me to move type definitions out of the function, and into
the unnamed namespace.
> * It's more work to implement than just writing a local sizeof.
> (This work can involve convincing someone that it's worth having
> this function in some project-wide header.)
If you do this just once, maybe. I have this in a project-wide
header already. In my case, I didn't have to convince anyone,
I just did it

. But in most organizations, you're right---the
politics can represent some effort.
The problem is that you need it anyway if you're doing any
generic programming. (I know, most organizations tend to avoid
templates, because of the problems they cause, but you never
know.) A version of global functions begin, end and size which
are overloaded to handle both built-in arrays and standard
containers, so that you're generic code works with both. (I.e.
in generic code, you don't write container.size(), but size(
container ). So that it can be made to work with types other
than the standard containers.)
> Now, if SomeOne could a find a way to detect whether a type is
> local or not...
> However, as far as I know that's impossible in C++98.
I think it will still be impossible in C++0x. Hopefully,
however, it won't matter---you should be able to do everything
with a local type that you can do with other types. (I don't
have access to a copy of the CD on this machine, however, to
verify.)
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34