Is there a way to tell if a macro paramerter is managed?

G

Guest

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?
 
H

Hendrik Schober

mccoyn said:
[...]
#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/[email protected]
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.

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top