Take the 2-minute tour ×

I know there are other questions like this but they are not very clear.

Now I feel as though this is a stupid question because I'm sure I already have the answer but let me ask anyways.

So I basically have a function that takes in a string and then based on the appropriate variable type it converts it like so:

template<class T>
void ConvertValue(std::string str, T &variable)
    variable = static_cast<T>(str);

so this seems to be fine correct? But the thing is that you cannot convert a string to say an int or a float so therefore I would have to do template specialization for ints and floats and for other types it can't convert to so what I'm asking is should I have something like this:

void ConvertValue(std::string str, int &variable) { variable = atoi(str.c_str()); }
void ConvertValue(std::string str, float &variable) { ... }
void ConvertValue(std::string str, double &variable) { ... }
void ConvertValue(std::string str, std::vector<int> &variable) { ... }


or should I use template specialization? Which one would make more sense? I'm leaning towards function overloading because majority of the types are going to have their own conversion function so since they slightly differ function overloading makes logical sense to me but I don't know if I'm missing something.

Should I stick with function overloading? Or switch to template specialization?

share|improve this question
I would not assume static-casting a by-val std::string to an arbitrary typed variable with even work, much less be "fine". But you can likely accomplish what you want with some help from a strstream. –  WhozCraig Apr 30 '13 at 23:01
It seems like an odd things to do. What would you expect to happen if you say, converted a string to a list. A list<string> ? A list<char> with each entry one of the strings chars? How would this handle user created objects? Seems like stringstream should take care of standard ints, floats, etc... Perhaps a templated container version if you needed. But what are you hoping to achieve here exactly? –  Muckle_ewe Apr 30 '13 at 23:03
add comment

4 Answers

If the internals of the function are going to have to be different for each type and potentially include type checks, it is simpler and cleaner to just have multiple functions.

If on the other hand you had a bunch of classes with a toString() method for conversion. Then you would use a template because the internals would always be the same.

share|improve this answer
+1 for suggesting to use a template if code reuse is the main goal. –  Jonny Dee Apr 30 '13 at 23:03
Ok I thought so thanks for the clarification –  CodingMadeEasy Apr 30 '13 at 23:05
add comment

I would use std::istringstream:

template <typename T>
T ConvertValue(const std::string& str)
    std::istringstream iss(str);

    T ret;
    if (!(iss >> ret))
        throw std::bad_cast("Failed!");

    return ret;
share|improve this answer
add comment

This should be the answer to your questions :

how to get typeof variable in C++

and yes it should be template specialization.

share|improve this answer
add comment

Short answer :

You are right. Function Overloading makes more sense. Function base template specialization does not overload.

Long Answer

function base template specializations are class 2 citizen, functions are first class citizen.

If you write an overloaded function nobody can write anything overloading or hiding your code without getting a compiler error. If you write a function base template specialization anybody can overload it with a function overload matching that function base template specialization signature. (yes, the programmer writing the specialization will be pissed of at your function, but will have to live with that).

If you want a better question for a better answer, read Why Not Specialize Function Templates? : http://www.gotw.ca/publications/mill17.htm

share|improve this answer
add comment

Your Answer


