Is there a 'functiondef' (similar to a 'typedef')?

  • Thread starter Thread starter Peter Oliphant
  • Start date Start date
P

Peter Oliphant

I have defined a templated function:

template< typename dataT >
dataT Add_T( dataT x, dataT y ) { return (x+y) ; }

Now, I'd like to do something like:

typedef Add_T<int> Add_int ;

That is, turn the ugly " Add_T<int>( x, y )" into something like "Add_int(x,
y )" (where 'x' and 'y' are 'int's). This is how it's done with templated
class definitions. How is it done with templated functions?
 
Peter said:
I have defined a templated function:

template< typename dataT >
dataT Add_T( dataT x, dataT y ) { return (x+y) ; }

Now, I'd like to do something like:

typedef Add_T<int> Add_int ;

That is, turn the ugly " Add_T<int>( x, y )" into something like
"Add_int(x, y )" (where 'x' and 'y' are 'int's). This is how it's
done with templated class definitions. How is it done with templated
functions?

You're almost out of luck - there's no such thing as a "funcdef".

What you could do though...

int (*Add_int)(int,int) = &Add_T<int>(int,int);

That declares a pointer to function taking (int,int) and returning int named
pAdd_T, and that pointer references the instantiation of your Add_T function
template.

You can call through a function pointer as-if it were a function:

int i = Add_int(3,5);

The downside is that using this technique will almost certainly prevent
Add_T<int> from being inlined when it's called through Add_int.

-cd
 
Thanx, Carl! : )

[==P==]

Carl Daniel said:
You're almost out of luck - there's no such thing as a "funcdef".

What you could do though...

int (*Add_int)(int,int) = &Add_T<int>(int,int);

That declares a pointer to function taking (int,int) and returning int
named pAdd_T, and that pointer references the instantiation of your Add_T
function template.

You can call through a function pointer as-if it were a function:

int i = Add_int(3,5);

The downside is that using this technique will almost certainly prevent
Add_T<int> from being inlined when it's called through Add_int.

-cd
 
template said:
dataT Add_T( dataT x, dataT y ) { return (x+y) ; }

Now, I'd like to do something like:

typedef Add_T<int> Add_int ;

That is, turn the ugly " Add_T<int>( x, y )" into something like
"Add_int(x, y )" (where 'x' and 'y' are 'int's). This is how it's done
with templated class definitions. How is it done with templated functions?

I think you can call this function without angle brackets: Add_T(int_x,
int_y).
 
I tried it, and you're right! I guess that 'int' is what it assumes as a
default typename if not given one... Cool to know! : )

[==P==]
 
I tried it, and you're right! I guess that 'int' is what it assumes as a
default typename if not given one... Cool to know! : )

Not exactly. The template type parameters are deduced from the function
arguments. You only need to specify them yourself using the f<type>()
syntax if the template parameter isn't represented in the function argument
list, or it is, and it can't be deduced for some reason, such as calling
f(x, y), where both parameters have the type T, but x is (say) an int and y
is a long and thus aren't the same type. To fix that, you can say f<int>(x,
y).
 
Interesting, and thanks for the explanation! : )

But then, how do you explain that the following works:

int result = Add_T( 1, 2 ) ;

Here the parameters are not 'typed', since '1' and '2' could be 'long' or
'int' or 'unsigned' etc. Although I'm guessing that, as parameters,
constants of the form of an integer without further casting will be assumed
to be an 'int'. Or it could be grabbing the parameter type from the 'result'
type perhaps (since it is structured to be the same type in as out)?

[==P==]
 
Interesting, and thanks for the explanation! : )

But then, how do you explain that the following works:

int result = Add_T( 1, 2 ) ;

Here the parameters are not 'typed', since '1' and '2' could be 'long' or
'int' or 'unsigned' etc. Although I'm guessing that, as parameters,
constants of the form of an integer without further casting will be assumed
to be an 'int'. Or it could be grabbing the parameter type from the 'result'
type perhaps (since it is structured to be the same type in as out)?

But 1 and 2 are integer literals that have the type int. For more on this,
see:

C++ Integer Constants
http://msdn.microsoft.com/library/d...lang_C.2b2b_.Integer_Constants.asp?frame=true

There are even more obscure rules that aren't covered there, that determine
the type when the constant won't fit in its "preferred" type, and this
further depends on the form of the constant, e.g. decimal vs. hex, and
whether or not there's a suffix. Here's a page that covers that:

http://publib.boulder.ibm.com/infoc...=/com.ibm.xlcpp8a.doc/language/ref/conref.htm
 
Back
Top