C++ C++;:从字符串创建参数化指针

C++ C++;:从字符串创建参数化指针,c++,templates,C++,Templates,我有一个遗留接口,它以字符串的形式给我实例的类型,例如“int”、“float”等 我想出了两个函数来解决这个问题: template <typename T> T type_factory(const std::string& type_id) { if (!type_id.compare("long")) { long res; return res; } if (!type_id.compare("dou

我有一个遗留接口,它以字符串的形式给我实例的类型,例如“int”、“float”等

我想出了两个函数来解决这个问题:

template <typename T>
T type_factory(const std::string& type_id)
{
    if (!type_id.compare("long"))
    {
        long res;
        return res;
    }

    if (!type_id.compare("double"))
    {
        double res;
        return res;
    }

    if (!type_id.compare("char"))
    {
        char res;
        return res;
    }
}
模板
T类型工厂(常数标准::字符串和类型id)
{
如果(!type_id.compare(“long”))
{
长余弦;
返回res;
}
如果(!type_id.compare(“double”))
{
双res;
返回res;
}
如果(!type_id.compare(“char”))
{
char res;
返回res;
}
}

模板
指针类*指针工厂(无效*ptr,T虚拟类型)
{
返回静态_-cast(ptr);
}
//指定用途:
//无效*原始ptr;
//计算器*p=指针工厂(原始ptr,类型工厂(“int”);
第二个函数没有编译,错误是PointerClass附近的“expected unqualified id”

有人能告诉我为什么第二个函数不能编译以及如何修复它吗


提前感谢。

您似乎需要一个模板:

template < template<typename T> class PointerClass, typename T>
PointerClass<T>* pointer_factory(void* ptr, T dummy_type) 
template