简体   繁体   English

模板专业化或功能重载

[英]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: 但问题是您无法将字符串转换为int或float,因此我将不得不对int和float以及其他无法转换为其他类型的模板进行特殊化处理,所以我要问的是像这样的东西:

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. 另一方面,如果您有一堆带有toString()方法进行转换的类。 Then you would use a template because the internals would always be the same. 然后,您将使用模板,因为内部构造将始终相同。

I would use std::istringstream : 我会用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++ 如何在C ++中获取typeof变量

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. 功能库模板专业是2类公民,功能是1类公民。

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 http : //www.gotw.ca/publications/mill17.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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