![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Call me Chuck
|
Function templates...
Is it possible to make a Template Function that doesn't know what return type it'll need to use until the function has actually ran? For instance:
C++ Syntax (Toggle Plain Text)
Would this be legal? Thanks
__________________
sqrt(-1) <3 3.14 98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature |
|
|
|
|
|
#2 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5
![]() |
Re: Function templates...
In your example, the type would be known at compile time, as that's when templates are converted to 'hard' code (I don't know the proper term for this). I am assuming that in your example, both return values are implicitly and unambiguously convertible to type T (if not, you'll have a compile-time error). If you want something that can return multiple types, you can do this so long as they share a common base class. For example, given the class
mammal which has cat, dog, and ferret as derived classes, you can have the method return mammal, and actually return one of the subclasses.Of course, this is of less use than you might think, since unless you know the exact type (you'd need RTTI for this), you cannot safely call any of the methods specific to the derived type.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#3 |
|
Call me Chuck
|
Re: Function templates...
Thanks, so what you're saying is that I need to let the template know what I want before compile time. lol, I guess the only way to do this now is program overload functions for basic types.
Any more tips? Thanks again for the help.
__________________
sqrt(-1) <3 3.14 98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature |
|
|
|
|
|
#4 |
|
Call me Chuck
|
Re: Function templates...
Perhaps you could help me with this particular function I'm having a problem with:
C++ Syntax (Toggle Plain Text)
I keep on getting an error saying that the compiler could not deduce a template argument for T. Any help would be appreciated, thanks.
__________________
sqrt(-1) <3 3.14 98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4
![]() |
Re: Function templates...
I dont think that templates are going to help you here.
What you are doing is trying to return multiple different values/types from the same function. void will never be a bool will never be a string. A function has to return only one type - regardless if that type is not known until compile-time (as is the case with templates). You have a couple of options: you could wrap all the types you expect to return into a class/struct, you could pass in a list of callback functions to be executed for each menu item you encounter or you could define a set of return values that represent action to be taken based on what happened. I personally like the callback approach since it is clean and the most flexible, but any of the above should work for you. Also, you have code like this cpp Syntax (Toggle Plain Text)
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#6 |
|
Call me Chuck
|
Re: Function templates...
Thanks, I think I know how to fix it now!
__________________
sqrt(-1) <3 3.14 98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Assigning a class function as a callback function | core8583 | C++ | 4 | Jun 18th, 2008 8:20 PM |
| Function Templates: Why use explicit specializations? | aznluvsmc | C++ | 3 | Apr 20th, 2006 7:28 AM |
| change the empty function from the old format to the new format | powah | Sed and Awk | 0 | Jun 23rd, 2005 12:10 PM |
| Sort function vs. sort_heap function + Transform Function | Josef_Stalin | C++ | 2 | May 2nd, 2005 12:18 PM |
| Returning a value from a variable to the main function | colt | C | 3 | Apr 28th, 2005 8:56 AM |