Connecting Tech Pros Worldwide Help | Site Map

function template

hopangtsun@gmail.com
Guest
 
Posts: n/a
#1: May 16 '06
Hi all, I have encountered a problem on using the function template
my goal is to add two numbers, which they can be int or double if I do
this in this way template<class T> T addition(T a, T b){ return (a
+ b); } it can only deal with int + int or double + double however,
I also want int + double, double + int so I come up with this idea
template<class T , class U> T addition(T a, U b){ return (a+b); }
this created a problem that it will return the type of the first
parameter for example: int + double would return an
int!!!(incorrect) 2 + 3.3 = 5 X double + int would return a
double!!!(correct) 3.3 + 2 = 5.3 so I would like to know is there any
methods that it can return the type correctly using the function
template? Thanks

Tomás
Guest
 
Posts: n/a
#2: May 16 '06

re: function template


posted:
[color=blue]
> Hi all, I have encountered a problem on using the function template
> my goal is to add two numbers, which they can be int or double if I do
> this in this way template<class T> T addition(T a, T b){ return (a
> + b); } it can only deal with int + int or double + double however,
> I also want int + double, double + int so I come up with this idea
> template<class T , class U> T addition(T a, U b){ return (a+b); }
> this created a problem that it will return the type of the first
> parameter for example: int + double would return an
> int!!!(incorrect) 2 + 3.3 = 5 X double + int would return a
> double!!!(correct) 3.3 + 2 = 5.3 so I would like to know is there any
> methods that it can return the type correctly using the function
> template? Thanks[/color]


I recall having this problem before. Essentially, you want all the
flexibility of a macro, e.g.:

#define addition(a,b) (a + b)


This macro function has the following benefits:

1) The return type is determined automagically.

2) You won't lose L-valueness. (if it indeed results in an L-value)


Functions, even template functions, unfortunately, still can't live up to
macro functions.

The round-about equivalent of what you want would be:

template<class A, class B>
typeof( a + b ) addition( A a, B b )
{
return a + b;
}

But template arguments don't drag an object's "reference status" or "L-
value" status along with it... so this won't work exactly like the macro
function.

So, to my knowledge, there's no universal way of changing a macro function
into an equivalently functioning template function.

Ofcourse, you can deal with macros on a case by case basis, and you'll find
a suitable template-function equivalent for most of them -- but not all.
Here's an example of a macro function which can't be beaten by a template
function:

#define NUMELEM(a) (sizeof(a) / sizeof(*a))

I myself never use macro functions (okay... very, very, rarely). I'll write:

ManipulateArray( array, sizeof(a) / sizeof(*a) );

Instead of:

ManipulateArray( array, NUMELEM(a) );


-Tomás
Victor Bazarov
Guest
 
Posts: n/a
#3: May 16 '06

re: function template


hopangtsun@gmail.com wrote:[color=blue]
> Hi all, I have encountered a problem on using the function template
> my goal is to add two numbers, which they can be int or double if I
> do this in this way template<class T> T addition(T a, T b){
> return (a + b); } it can only deal with int + int or double +
> double however, I also want int + double, double + int so I come
> up with this idea template<class T , class U> T addition(T a, U b){
> return (a+b); } this created a problem that it will return the type
> of the first parameter for example: int + double would return an
> int!!!(incorrect) 2 + 3.3 = 5 X double + int would return a
> double!!!(correct) 3.3 + 2 = 5.3 so I would like to know is there any
> methods that it can return the type correctly using the function
> template?[/color]

I think you need some kind of type traits for that. Or you could supply
the third type to be used as the return value type:

template<class R, class T, class U> R addition(T a, U b) {
return a + b;
}
...
addition<float>(1, 'b');


V
--
Please remove capital As from my address when replying by mail


Rolf Magnus
Guest
 
Posts: n/a
#4: May 16 '06

re: function template


Tomás wrote:
[color=blue]
> I recall having this problem before. Essentially, you want all the
> flexibility of a macro, e.g.:
>
> #define addition(a,b) (a + b)
>
>
> This macro function has the following benefits:
>
> 1) The return type is determined automagically.[/color]

Which can also be a disadvantage.
[color=blue]
> 2) You won't lose L-valueness. (if it indeed results in an L-value)[/color]

That's right. There might be some template metaprogramming way around it,
but that would be less elegant.
[color=blue]
> Ofcourse, you can deal with macros on a case by case basis, and you'll
> find a suitable template-function equivalent for most of them -- but not
> all. Here's an example of a macro function which can't be beaten by a
> template function:
>
> #define NUMELEM(a) (sizeof(a) / sizeof(*a))[/color]

You mean like this?

template<typename T, int n> size_T numelem(T(&arr)[n])
{
return n;
}

This one has the benefit that it won't work with pointers, only arrays,
while the macro version produces garbage if you give it a pointer.

Tomás
Guest
 
Posts: n/a
#5: May 16 '06

re: function template


[color=blue]
> template<typename T, int n> size_T numelem(T(&arr)[n])
> {
> return n;
> }[/color]


That doesn't yield a compile-time constant, and it doesn't work with classes
defined within a function.

int array[5];

int main()
{
char monkey[ numelem(array) ]; /* Error: not constant */

class Ape {};

Ape apes[7];

numelem(ape); /* Error: class defined in function */
}


Alf P. Steinback wrote a good article about this. Here's the link:

http://www.google.com/url?
sa=D&q=http://home.no.net/dubjai/win32cpptut/special/pointers/array_size.doc
..pdf

[color=blue]
> This one has the benefit that it won't work with pointers, only arrays,
> while the macro version produces garbage if you give it a pointer.[/color]

Indeed, so you have make sure you've got an array.


-Tomás

Tomás
Guest
 
Posts: n/a
#6: May 16 '06

re: function template



[color=blue]
> Alf P. Steinback wrote a good article about this.[/color]


Apologise for the name mangling: Alf P. Steinbach


-Tomás
JH Programmer
Guest
 
Posts: n/a
#7: May 16 '06

re: function template


Thanks Victor
I also come up with the same idea with you.

However, due to the limitation of the program

they only provide get_int() and get_double() for me
which I need to know whether it is int or double before I get the
private member

so I think checking the first operand is esstential
however, the instructor told us we can't do this.

Can Dynamic casting help in this situation?

Victor Bazarov
Guest
 
Posts: n/a
#8: May 16 '06

re: function template


JH Programmer wrote:[color=blue]
> Thanks Victor
> I also come up with the same idea with you.
>
> However, due to the limitation of the program
>
> they only provide get_int() and get_double() for me
> which I need to know whether it is int or double before I get the
> private member
>
> so I think checking the first operand is esstential
> however, the instructor told us we can't do this.
>
> Can Dynamic casting help in this situation?[/color]

No. 'dynamic_cast' only works for polymorphic UDTs.

V
--
Please remove capital As from my address when replying by mail


Closed Thread