473,320 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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?

Nov 17 '05 #1
7 989
Peter Oliphant wrote:
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
Nov 17 '05 #2
Thanx, Carl! : )

[==P==]

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uT**************@TK2MSFTNGP12.phx.gbl...
Peter Oliphant wrote:
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

Nov 17 '05 #3
> 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?


I think you can call this function without angle brackets: Add_T(int_x,
int_y).

--
Vladimir Nesterovsky
Nov 17 '05 #4
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==]

"Vladimir Nesterovsky" <vl******@nesterovsky-bros.com> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...
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?


I think you can call this function without angle brackets: Add_T(int_x,
int_y).

--
Vladimir Nesterovsky

Nov 17 '05 #5
On Thu, 20 Oct 2005 09:08:16 -0700, "Peter Oliphant"
<po*******@RoundTripInc.com> wrote:
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).

--
Doug Harrison
Visual C++ MVP
Nov 17 '05 #6
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==]

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:om********************************@4ax.com...
On Thu, 20 Oct 2005 09:08:16 -0700, "Peter Oliphant"
<po*******@RoundTripInc.com> wrote:
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).

--
Doug Harrison
Visual C++ MVP

Nov 17 '05 #7
On Thu, 20 Oct 2005 13:11:46 -0700, "Peter Oliphant"
<po*******@RoundTripInc.com> wrote:
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/de...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/infoce...ref/conref.htm
--
Doug Harrison
Visual C++ MVP
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
5
by: Mr A | last post by:
Hi! I'm trying to do the following: emplate <typename Resource> class ResourceManager { public: typedef std::list<Resource*>::iterator Iterator; typedef std::list<Resource*>::const_iterator...
8
by: Fred L. Kleinschmidt | last post by:
I need to know the largets value representable in a variable. However, I do not know the variable's true type - only that it is some kind of int. It may be any of the following: #typedef Newtype...
14
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
6
by: Henrik Goldman | last post by:
Hello, I have a dataset which consist of a string username and string hostname as a key and then an integer representing a count as the matching "second" value in a pair. So far I've used...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.