| | |
Conditional template specialization
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hey guys,
How would I partially specilize a template in a manner as below? Is that even possible?
I have these two functions:
And I'd like to create a function like this:
How would I do that?
Thanks in advance,
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)
bool node::read(const char *filename) { ifstream f(filename); if (!f.good()) return false; bool success = read(f); f.close(); return success; } bool node::write(const char *filename) { ofstream f(filename); if (!f.good()) { return false; } bool success = write(f); f.close(); return success; }
And I'd like to create a function like this:
cpp Syntax (Toggle Plain Text)
template <typename T> bool node::read_or_write(const char *filename) { T f(filename); if (!f.good()) { return false; } bool success; if(T == ifstream){ success = write(f); } else if (T == ofstream){ success = read(f); } f.close(); return success; }
How would I do that?
Thanks in advance,
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:
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.
Code could look a bit like this:
cpp Syntax (Toggle Plain Text)
template <typename T = {ifstream, ofstream}> bool node::read_or_write(const char *filename) { T f(filename); if (!f.good()) { return false; } bool success; if(T == typename ifstream){ success = write(f); } else if (T == typename ofstream){ success = read(f); } f.close(); return success; }
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)
template <typename T> class filestuffs { filestuffs(const char *filename) : filename(filename) { f(filename); success = f.good(); success = read_or_write(); f.close(); } //can't do this unless we know T bool read_or_write(){ return false; } T f; bool success; const char *filename; const char [] errmsg = "Can't do this on anything but: ifstream, ofstream"; } template <> class filestuffs<ifstream> { //now we know T! :D bool read_or_write(){ return read(f); } } template<> class filestuffs<ofstream> { bool read_or_write(){ return write(f); } }
Last edited by Clockowl; Apr 25th, 2009 at 3:20 pm.
I am not looking for a runtime solution, that's why I hadn't.
Here's a working example:
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++.
Here's a working example:
cpp Syntax (Toggle Plain Text)
template <typename T> bool read_or_write(const char *filename) { T f(filename); if (!f.good()) { return false; } bool success; if (typeid(T) == typeid(ofstream)) { success = write(f); } else if (typeid(T) == typeid(ifstream)) { success = read(f); } f.close(); return success; }
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: intialization of array of objects
- Next Thread: plss help wid binary to decimal...
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






, but I asked why you 