我应该专攻还是超载?

fre*_*low 2 c++ templates overloading function template-specialization

假设我有一个功能模板:

template <typename T>
std::string foo(const T& x)
{
    return some_computation_involving(x);
}
Run Code Online (Sandbox Code Playgroud)

如果x已经是一个字符串,我只想逐字传递它.我应该专门化功能模板吗?

template <>
std::string foo(const std::string& x)
{
    return x;
}
Run Code Online (Sandbox Code Playgroud)

或者我应该提供非模板功能?

std::string foo(const std::string& x)
{
    return x;
}
Run Code Online (Sandbox Code Playgroud)

在什么情况下我应该选择哪个选项,以及我需要注意哪些陷阱?

Naw*_*waz 5

Herb Sutter表示,首选功能专业化过载.他在文章中解释了这一点: