|
-
July 2nd, 2009, 04:09 AM
#2
Re: Different return type depending on data
 Originally Posted by metzenes
Specifically, I'm reading from a text file. If the data found are strings of characters, I'd like to return a vector of strings. On the other hand, I'd like to return a vector of floats if the data are numbers.
OK.
I don't think this is possible with C++
Instead of thinking of other languages, you should concentrate on how to solve this using C++.
You are given a string, and you are doing something different when a different string is given to you. That is basically what this all boils down to. The problem is that your mindset is on "returning" something, when it could be solved by redesigning your code so that it doesn't need to return anything specific. Maybe that entire testing of the string shouldn't be in the template to begin with.
Second, your example is faulty -- you are returning a list<T>, when the return type is vector<T>.
But in general it is possible in C++ using something called "compile-time polymorphism" or policy-based programming. What you are looking for is something called "template specialization". Do a search on CodeGuru, and you will see samples of this.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 2nd, 2009 at 04:19 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|