Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Oliphant - 18 Oct 2005 00:08 GMT
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?
Carl Daniel [VC++ MVP] - 18 Oct 2005 03:13 GMT
> I have defined a templated function:
>
[quoted text clipped - 9 lines]
> 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
Peter Oliphant - 18 Oct 2005 17:32 GMT
Thanx, Carl! : )

[==P==]

>> I have defined a templated function:
>>
[quoted text clipped - 28 lines]
>
> -cd
Vladimir Nesterovsky - 20 Oct 2005 10:39 GMT
> template< typename dataT >
> dataT Add_T( dataT x, dataT y ) { return (x+y) ; }
[quoted text clipped - 6 lines]
> "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).

Signature

Vladimir Nesterovsky

Peter Oliphant - 20 Oct 2005 17:08 GMT
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==]

>> template< typename dataT >
>> dataT Add_T( dataT x, dataT y ) { return (x+y) ; }
[quoted text clipped - 10 lines]
> I think you can call this function without angle brackets: Add_T(int_x,
> int_y).
Doug Harrison [MVP] - 20 Oct 2005 17:19 GMT
>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).

Signature

Doug Harrison
Visual C++ MVP

Peter Oliphant - 20 Oct 2005 21:11 GMT
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==]

>>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! : )
[quoted text clipped - 9 lines]
> f<int>(x,
> y).
Doug Harrison [MVP] - 20 Oct 2005 22:18 GMT
>Interesting, and thanks for the explanation! : )
>
[quoted text clipped - 7 lines]
>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/default.asp?url=/library/en-us/vclang/html/_pl
uslang_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/infocenter/comphelp/v8v101/index.jsp?topic=/com.ib
m.xlcpp8a.doc/language/ref/conref.htm


Signature

Doug Harrison
Visual C++ MVP


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.