function like sizeof

Hi all ,

i want to create my own function which will work like sizeof().

i can find out size trough pointers.

but i don't know how to write such function or how to call it

my_sizeof(int) ?? i think this will not work . any idea ?

thanks in advance.
Can you post an example call to this hypothetical function? What do you expect to take as input and to return as output?
thanks helios for reply.

i want something just lilke sizeof .

can i write some function which can be called like this

my_sizeof(int); // sending int , long to function

returns 4 .

i think another example is va_arg( va_list argptr, type );

where we can send data types.

can we have our own functions like above one.

i am trying for own sizeof .






So... What's wrong with sizeof?
If you're trying to reimplement it, I have bad news for you: sizeof is built into the language. Any function that behaves as you describe will just be a variation of this:
1
2
3
4
5
6
template <typename T>
size_t my_sizeof(){
    return sizeof(T);
}

my_sizeof<int>();

Or maybe you just want to pass types as parameters. That's templates, in a way.
well , nothing wrong with sizeof

just want to create function which can take data type as argument ,with sizeof like as sample.

my_function(int); // possible ?? in c++

thanks helios





No, it's isn't possible using that syntax because C++ does not allow types to be passed as parameters to functions.

The best you could do is a macro front-end, but in the end it does nothing more than sizeof.

1
2
3
4
5
template< typename T >
size_t size_of()
    { return sizeof T; }

#define SIZE_OF( Type ) size_of<Type>() 




Thanks Jsmith.
Topic archived. No new replies allowed.