简体   繁体   中英

Template Specialization or Function Overloading

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) { ... }

..etc

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?

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.

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;
}

This should be the answer to your questions :

how to get typeof variable in C++

and yes it should be template specialization.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM