Conditional template specialization

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Conditional template specialization

 
0
  #1
Apr 25th, 2009
Hey guys,

How would I partially specilize a template in a manner as below? Is that even possible?

I have these two functions:

  1. bool node::read(const char *filename) {
  2. ifstream f(filename);
  3.  
  4. if (!f.good()) return false;
  5.  
  6. bool success = read(f);
  7. f.close();
  8.  
  9. return success;
  10. }
  11.  
  12. bool node::write(const char *filename) {
  13. ofstream f(filename);
  14.  
  15. if (!f.good()) {
  16. return false;
  17. }
  18.  
  19. bool success = write(f);
  20. f.close();
  21.  
  22. return success;
  23. }

And I'd like to create a function like this:

  1. template <typename T>
  2. bool node::read_or_write(const char *filename) {
  3. T f(filename);
  4.  
  5. if (!f.good()) {
  6. return false;
  7. }
  8.  
  9. bool success;
  10. if(T == ifstream){
  11. success = write(f);
  12. } else if (T == ofstream){
  13. success = read(f);
  14. }
  15. f.close();
  16.  
  17. return success;
  18. }

How would I do that?

Thanks in advance,
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Conditional template specialization

 
0
  #2
Apr 25th, 2009
I think you'll need RunTime Type Identification (RTTI) for that purpose ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Conditional template specialization

 
0
  #3
Apr 25th, 2009
Well, a design wouldn't need it. I don't know if its needed but can't you say something like: T can only be ifstream, ofstream

Code could look a bit like this:

  1. template <typename T = {ifstream, ofstream}>
  2. bool node::read_or_write(const char *filename) {
  3. T f(filename);
  4.  
  5. if (!f.good()) {
  6. return false;
  7. }
  8.  
  9. bool success;
  10. if(T == typename ifstream){
  11. success = write(f);
  12. } else if (T == typename ofstream){
  13. success = read(f);
  14. }
  15. f.close();
  16.  
  17. return success;
  18. }

Compiler should throw an error when you try to instantiate any other type of it.. I'm trying to create such a class now, but I'm not very good at it.

This is the best I could come up with, but I didn't compile/test it yet due to diner being ready.

  1. template <typename T>
  2. class filestuffs {
  3. filestuffs(const char *filename) : filename(filename) {
  4. f(filename);
  5. success = f.good();
  6. success = read_or_write();
  7. f.close();
  8. }
  9.  
  10. //can't do this unless we know T
  11. bool read_or_write(){ return false; }
  12.  
  13. T f;
  14. bool success;
  15. const char *filename;
  16. const char [] errmsg = "Can't do this on anything but: ifstream, ofstream";
  17. }
  18. template <>
  19. class filestuffs<ifstream> {
  20. //now we know T! :D
  21. bool read_or_write(){
  22. return read(f);
  23. }
  24. }
  25.  
  26. template<>
  27. class filestuffs<ofstream> {
  28. bool read_or_write(){
  29. return write(f);
  30. }
  31. }
Last edited by Clockowl; Apr 25th, 2009 at 3:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Conditional template specialization

 
0
  #4
Apr 25th, 2009
And what's the result ?
Did it work ?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Conditional template specialization

 
0
  #5
Apr 25th, 2009
Oh I'm sure that doesn't work, never seen it, just made it up. But I want to know how I'd do that in C++.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Conditional template specialization

 
0
  #6
Apr 25th, 2009
Did you try searching google (e.g: typeid , c++ rtti )?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Conditional template specialization

 
0
  #7
Apr 25th, 2009
I am not looking for a runtime solution, that's why I hadn't.

Here's a working example:
  1. template <typename T>
  2. bool read_or_write(const char *filename) {
  3. T f(filename);
  4. if (!f.good()) {
  5. return false;
  6. }
  7.  
  8. bool success;
  9. if (typeid(T) == typeid(ofstream)) {
  10. success = write(f);
  11. } else if (typeid(T) == typeid(ifstream)) {
  12. success = read(f);
  13. }
  14. f.close();
  15.  
  16. return success;
  17. }

But this way, I also have to define write(ifstream ... ), while I know 100% sure that the compiler will never have to call such a function. It kinda defeats it's use (making the code smaller) for this small function, but for bigger functions this sort of template is very usable I think.

Any compiler-time suggestions?

PS: For anyone reading this and thinking, why the hell are you creating such a function for 20 lines of code? It's because I'm not going to use read_or_write(), but would like to know how I'd accomplish maximum code re-usage in C++.
Last edited by Clockowl; Apr 25th, 2009 at 4:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Conditional template specialization

 
0
  #8
Apr 25th, 2009
Why are you using two separate functions read and write then ?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Conditional template specialization

 
0
  #9
Apr 25th, 2009
Originally Posted by tux4life
Why are you using two separate functions read and write then ?
Because one reads and one writes? Hahahaha
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Conditional template specialization

 
0
  #10
Apr 25th, 2009
Originally Posted by Clockowl View Post
Because one reads and one writes? Hahahaha
Of course , but I asked why you SEPERATED these from your function, why not implement them directly in your function ?
Last edited by tux4life; Apr 25th, 2009 at 4:31 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 648 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC