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)
在什么情况下我应该选择哪个选项,以及我需要注意哪些陷阱?