C++ 如何专门设计用于返回不同类型的模板函数?

C++ 如何专门设计用于返回不同类型的模板函数?,c++,function,templates,C++,Function,Templates,我目前有一个函数模板,它读取从配置文件获得的字符串,创建并返回指向派生类的新实例的共享指针: template <typename T> std::shared_ptr<T> foo(std::string& info) { /* */ } template std::shared\u ptr foo(std::string&info) { /* */ } 当我返回一个特定的类时,我想专门处理这个问题,因为它需要以不同的方式处理字符串,但是我找不到一种方法来处

我目前有一个函数模板,它读取从配置文件获得的字符串,创建并返回指向派生类的新实例的共享指针:

template <typename T> std::shared_ptr<T> foo(std::string& info)
{ /* */ }
template std::shared\u ptr foo(std::string&info)
{ /* */ }
当我返回一个特定的类时,我想专门处理这个问题,因为它需要以不同的方式处理字符串,但是我找不到一种方法来处理它。 尝试

std::shared\u ptr foo(std::string&info)
{/* */}

template std::shared\u ptr foo(std::string&info)
{/* */}
两者都失败了,因为我似乎违反了一个定义规则。 我也试过了

template <> std::shared_ptr<Derived> foo<std::shared_ptr<Derived>>(std::string& info)
{/* */}
template std::shared\u ptr foo(std::string&info)
{/* */}

但是我收到了Visual Studio错误消息C2192(显式专门化“声明”不是函数模板的专门化)。

您只需要在标识符之后提供模板参数。您上次的尝试已结束,但模板参数是
派生的
不是
std::shared\u ptr

template std::shared\u ptr foo(std::string&info)
//在此处添加模板参数^^^^^^^
简化示例:

template <> std::shared_ptr<Derived> foo(std::string& info)
{/* */}
template <> std::shared_ptr<Derived> foo<std::shared_ptr<Derived>>(std::string& info)
{/* */}
template <> std::shared_ptr<Derived> foo<Derived>(std::string& info)
//        Add the template argument here ^^^^^^^