I have a macro that can determine the number of elements in an array type. It is defined as follows
#define ARRAY_SIZE_NOGC(x) (sizeof(x) / sizeof(x[0])
This works with unmanaged array types, but not with pointers, since the size of a pointer is always the same and not the size of the data it points to. Obviously this doesn't work with managed types, which are all refered to by pointers. I could define the macro to handle managed arrays as follows
#define ARRAY_SIZE_GC(x) (x->Length
The problem is these two macros arn't generic. I would like one macro that would work for both. Something like the following
#define ARRAY_SIZE(x) ( IS_A_GC_POINTER(x) ? ARRAY_SIZE_GC(x) : ARRAY_SIZE_NOGC(x)
Is there a way to implement the IS_A_GC_POINTER macro above?
> [...]
> #define ARRAY_SIZE(x) ( IS_A_GC_POINTER(x) ? ARRAY_SIZE_GC(x) : ARRAY_SIZE_NOGC(x) )
>
> Is there a way to implement the IS_A_GC_POINTER macro above?
I have no idea of MC++, but if you don't need
this information at compile-time, then maybe
overloading at run-time would help? Look if
this
http://groups.google.com/groups?selm=6ntce1%2445f%241%40nnrp1.dejanews.com
solution gets you somewhere.
Schobi
P.S.: Oh, I'm just thinking that if you use a
template class, instead of the template
function, you should get the size at
compile-time as well. Something like
this (uncompiled code!)
template< typename T >
struct ArraySize; // undefined
template< typename T, CCW_CSTD::size_t sz >
struct ArraySize<T[sz]> {enum {result=sz};}
might do.

Signature
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org
"Sometimes compilers are so much more reasonable than people."
Scott Meyers