Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 5th, 2008, 3:23 AM   #1
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
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)
  1. template<typename T>
  2. T example(int i)
  3. {
  4. if(i == 1)
  5. return "Nope";
  6. else
  7. return 16.23213;
  8. }

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
ShawnStovall is offline   Reply With Quote
Old Aug 5th, 2008, 5:04 AM   #2
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5 lectricpharaoh will become famous soon enough
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
lectricpharaoh is offline   Reply With Quote
Old Aug 5th, 2008, 5:13 AM   #3
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
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
ShawnStovall is offline   Reply With Quote
Old Aug 5th, 2008, 5:26 AM   #4
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: Function templates...

Perhaps you could help me with this particular function I'm having a problem with:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "QuizClass.h"
  3. #include <fstream>
  4. #include <cstring>
  5. #include <cctype>
  6. #include <vector>
  7. using std::cout;
  8. using std::cin;
  9. using std::endl;
  10. using std::ifstream;
  11. using std::ofstream;
  12. using std::string;
  13. using std::vector;
  14.  
  15. void stolower(string& str);
  16. template<typename T>
  17. T menu(string cmd)
  18. {
  19. if(cmd[0] == '\\')
  20. {
  21. unsigned j = 0;
  22. string stemp;
  23.  
  24. for(unsigned i = 1; i <= cmd.length(); i++)
  25. {
  26. stemp[j] = cmd[i];
  27. j++;
  28. }
  29.  
  30. string ltemp = stemp;
  31. stolower(stemp);
  32.  
  33. cmd.clear();
  34. cmd = stemp;
  35.  
  36. if(cmd == "help")
  37. cout << "Commands:\n"
  38. "[ADD FILE] This will open up a prompt for adding a file path.\n"
  39. "[REMOVE FILE] This will open a prompt for deleting a file path.\n"
  40. "[CLEAR FILES] This will erase all paths to previously added files.\n"
  41. "[CONTINUE] This will let you continue the questioning proccess after initiating a prompt.\n"
  42. "[HELP] This will display the list of commands.\n"
  43. "[RESTART] This will restart the application.\n"
  44. "[QUIT] This will close the program.\n"
  45. "\nYou may enter these commands at any time.\n"
  46. "You can use \"\\\" to override a command or any answer that uses the \"\\\", e.g. \\\\one = \\one."
  47. "You must enter the commands with a \"\\\" preceding it in order for it to work.\n\n";
  48. else if(cmd == "add file")
  49. {
  50. vector<string> pthhld;
  51. ifstream pathst("paths.pth");
  52. if(!pathst)
  53. {
  54. pathst.close();
  55. ofstream ptemp("paths.pth");
  56. ptemp.close();
  57. pathst.open("paths.pth");
  58. }
  59.  
  60. while(!pathst.eof())
  61. {
  62. string temp;
  63. std::getline(pathst, temp);
  64. pthhld.push_back(temp);
  65. temp.clear();
  66. }
  67. pathst.close();
  68. ofstream pathsf("paths.pth", std::ios::trunc);
  69.  
  70. cout << "\nPlease enter the path to the file: ";
  71. string temp;
  72. std::getline(cin, temp);
  73. if(menu(temp) == true || menu(temp) == false)
  74. return menu(temp);
  75. pthhld.push_back(temp);
  76.  
  77. for(unsigned i = 0; i < pthhld.size(); i++)
  78. pathsf << pthhld[i] << endl;
  79.  
  80. pathsf.close();
  81. }
  82. else if(cmd == "remove file")
  83. {
  84. vector<string> pthhld;
  85. ifstream pathst("paths.pth");
  86. if(!pathst || pathst.eof())
  87. {
  88. cout << "Error! File is either empty or it does not exist! Exiting prompt...\n";
  89. pathst.close();
  90. return;
  91. }
  92. while(!pathst.eof())
  93. {
  94. string temp;
  95. std::getline(pathst, temp);
  96. pthhld.push_back(temp);
  97. temp.clear();
  98. }
  99. pathst.close();
  100.  
  101. cout << "Please choose which path to remove:\n";
  102. for(int i = 0; i < pthhld.size(); i++)
  103. cout << i + 1 << "." << pthhld[i];
  104.  
  105. int temp;
  106. cin >> temp;
  107. if(menu(temp) == true || menu(temp) == false)
  108. return menu<bool>(temp);
  109. pthhld.erase(temp - 1);
  110. cout << "Path erased.\nProgram restart needed for effects to be seen.\n\n";
  111.  
  112. ofstream pathsf("paths.pth", std::iso::trunc);
  113. for(unsigned i = 0; i < pthhld.size(); i++)
  114. pathsf << pthhld[i] << endl;
  115.  
  116. pathsf.close();
  117. }
  118. else if(cmd == "clear files")
  119. {
  120. ofstream pathsf("paths.pth", std::iso::trunc);
  121. pathsf.close();
  122. }
  123. else if(cmd == "restart")
  124. return 2;
  125. else if(cmd == "continue")
  126. return true;
  127. else if(cmd == "quit")
  128. return false;
  129. else
  130. return ltemp;
  131. }
  132. else
  133. return cmd;
  134. }

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
ShawnStovall is offline   Reply With Quote
Old Aug 5th, 2008, 7:14 AM   #5
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4 L7Sqr is on a distinguished road
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)
  1. if(menu(temp) == true || menu(temp) == false)
It seems that you are coming from a dynamic language such as Python or Ruby where a method can return anything. In c++, a statement like that will always evaluate to true since a call to a boolean function can only return one of those two values (and if menu did not return a bool you would get a compile-time error).
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]
L7Sqr is offline   Reply With Quote
Old Aug 5th, 2008, 2:51 PM   #6
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
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
ShawnStovall is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:43 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC