943,987 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1807
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 25th, 2009
0

Conditional template specialization

Expand Post »
Hey guys,

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

I have these two functions:

cpp Syntax (Toggle Plain Text)
  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:

cpp Syntax (Toggle Plain Text)
  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,
Similar Threads
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 25th, 2009
0

Re: Conditional template specialization

I think you'll need RunTime Type Identification (RTTI) for that purpose ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 25th, 2009
0

Re: Conditional template specialization

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:

cpp Syntax (Toggle Plain Text)
  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.

cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 25th, 2009
0

Re: Conditional template specialization

And what's the result ?
Did it work ?
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 25th, 2009
0

Re: Conditional template specialization

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++.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 25th, 2009
0

Re: Conditional template specialization

Did you try searching google (e.g: typeid , c++ rtti )?
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 25th, 2009
0

Re: Conditional template specialization

I am not looking for a runtime solution, that's why I hadn't.

Here's a working example:
cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 25th, 2009
0

Re: Conditional template specialization

Why are you using two separate functions read and write then ?
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 25th, 2009
0

Re: Conditional template specialization

Quote originally posted by tux4life ...
Why are you using two separate functions read and write then ?
Because one reads and one writes? Hahahaha
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 25th, 2009
0

Re: Conditional template specialization

Click to Expand / Collapse  Quote originally posted by Clockowl ...
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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: intialization of array of objects
Next Thread in C++ Forum Timeline: plss help wid binary to decimal...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC