write a function such that when ever i call this function in some other function .it should give me

K

komal

hi all
basically my problem is i have to write a function such that when ever
i call this function in some other function .it should give me tha data
type and value of calling function parameter.and no of parameter is
calling function can be anything.
for example.suppose my function is function2.
then when i call
function1(int i ,char j,float d)
{
function2()
}

ouput should be
i is integer
j is char
d is float

and data type can be anything
 
B

Bob Hairgrove

hi all
basically my problem is i have to write a function such that when ever
i call this function in some other function .it should give me tha data
type and value of calling function parameter.and no of parameter is
calling function can be anything.
for example.suppose my function is function2.
then when i call
function1(int i ,char j,float d)
{
function2()
}

ouput should be
i is integer
j is char
d is float

and data type can be anything

I question the wisdom or usefulness of doing this. Sounds like a
homework assignment to me.

Anyway, you can try using the ellipsis and va_arg, but there are
certain restrictions as to the types of arguments you can pass. Most
importantly, the standard says that invoking va_arg on non-POD class
types results in undefined behavior (section 5.2.2 part 7). To use
va_arg, you need to include <cstdarg>.

Also, check out the std::type_info class for non-POD class RTTI.
Include the header <typeinfo> to use this class.
 
M

msalters

komal said:
hi all
basically my problem is i have to write a function such that when ever
i call this function in some other function .it should give me tha data
type and value of calling function parameter.and no of parameter is
calling function can be anything.
for example.suppose my function is function2.
then when i call
function1(int i ,char j,float d)
{
function2()
}

ouput should be
i is integer
j is char
d is float

Impossible. function2 doesn't get any information about function1.
At the very least, you should write it as

f1(int i ,char j,float d) { f2( &f1 ); }

In that case, f2 can be a template. With Template Argument Deduction
you can then at least know the types. Still, the variable names are
only for the compiler and not available at runtime.
Regards,
Michiel Salters
 
M

msalters

komal said:
hi all
basically my problem is i have to write a function such that when ever
i call this function in some other function .it should give me tha data
type and value of calling function parameter.and no of parameter is
calling function can be anything.
for example.suppose my function is function2.
then when i call
function1(int i ,char j,float d)
{
function2()
}

ouput should be
i is integer
j is char
d is float

Impossible. function2 doesn't get any information about function1.
At the very least, you should write it as

f1(int i ,char j,float d) { f2( &f1 ); }

In that case, f2 can be a template. With Template Argument Deduction
you can then at least know the types. Still, the variable names are
only for the compiler and not available at runtime.
Regards,
Michiel Salters
 
K

Karl Heinz Buchegger

Bob said:
I question the wisdom or usefulness of doing this. Sounds like a
homework assignment to me.

Anyway, you can try using the ellipsis and va_arg, but there are
certain restrictions as to the types of arguments you can pass. Most
importantly, the standard says that invoking va_arg on non-POD class
types results in undefined behavior (section 5.2.2 part 7). To use
va_arg, you need to include <cstdarg>.

And it wouldn't help anyway.
When working with va_arg you already need to know the type of the
passed arguments. That's one reason why printf has those fancy %d, %f
&c, %s, ... formatting flags.
 
M

msalters

komal said:
hi all
basically my problem is i have to write a function such that when ever
i call this function in some other function .it should give me tha data
type and value of calling function parameter.and no of parameter is
calling function can be anything.
for example.suppose my function is function2.
then when i call
function1(int i ,char j,float d)
{
function2()
}

ouput should be
i is integer
j is char
d is float

Impossible. function2 doesn't get any information about function1.
At the very least, you should write it as

f1(int i ,char j,float d) { f2( &f1 ); }

In that case, f2 can be a template. With Template Argument Deduction
you can then at least know the types. Still, the variable names are
only for the compiler and not available at runtime.
Regards,
Michiel Salters
 
M

msalters

komal said:
hi all
basically my problem is i have to write a function such that when ever
i call this function in some other function .it should give me tha data
type and value of calling function parameter.and no of parameter is
calling function can be anything.
for example.suppose my function is function2.
then when i call
function1(int i ,char j,float d)
{
function2()
}

ouput should be
i is integer
j is char
d is float

Impossible. function2 doesn't get any information about function1.
At the very least, you should write it as

f1(int i ,char j,float d) { f2( &f1, i,j,d ); }

In that case, f2 can be a template. With Template Argument Deduction
you can then at least know the types. Still, the variable names are
only for the compiler and not available at runtime.
Regards,
Michiel Salters
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top