C++ 如何重载模板函数

C++ 如何重载模板函数,c++,c++11,c++14,C++,C++11,C++14,我试图在类中重载模板函数,但没有成功 如何在代码中重载模板 class CommandInterfaceFont { private: std::map < std::string, FontParam > lookUpMap; public: CommandInterfaceFont(); friend class SumCommandInterface; template <typename plug , typename typeVal

我试图在类中重载模板函数,但没有成功

如何在代码中重载模板

class CommandInterfaceFont
{
private:
    std::map < std::string, FontParam > lookUpMap;

public:
    CommandInterfaceFont();
    friend class SumCommandInterface;

    template <typename plug , typename typeValue >
    void SetValue(plug* geom, std::string stdstrParamName, typeValue value);

};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <typename plug >
void CommandInterfaceFont::SetValue(plug* geom, std::string stdstrParamName, std::string value)
{       
            geom->setText(value);       
}

template <typename plug >
void CommandInterfaceFont::SetValue(plug* geom, std::string stdstrParamName, bool value)
{   
            geom->setUseShadow(value);      
}
而不是

样板 void SetValueplug*geom,std::string stdstrParamName,typeValue; 如下所示声明成员函数:

样板 void SetValueplug*geom,std::string stdstrParamName,std::string value; 样板 void SetValueplug*geom,std::string stdstrParamName,bool值; 如果重载是你想要的。您的示例看起来更像。

而不是

样板 void SetValueplug*geom,std::string stdstrParamName,typeValue; 如下所示声明成员函数:

样板 void SetValueplug*geom,std::string stdstrParamName,std::string value; 样板 void SetValueplug*geom,std::string stdstrParamName,bool值; 如果重载是你想要的。您的示例看起来更像。

除了回答:甚至可能有所有三个重载,一个接受bool、std::string和一个泛型重载。在这个非常具体的用例中,它可能没有意义,但您需要自己决定,但为了完整性,请将其视为更一般的扩展,我仍将展示:

class CommandInterfaceFont
{
public:

    template <typename plug , typename typeValue >
    void setValue(plug* geom, std::string stdstrParamName, typeValue value)
    {
        // does something generic, whatever this would be
    }

    template <typename plug >
    void setValue(plug* geom, std::string stdstrParamName, std::string value)
    {
        // specific
    }

    template <typename plug >
    void setValue(plug* geom, std::string stdstrParamName, bool value)
    {
        // specific
    }
};
要做到这一点,关键在于您至少需要声明类范围中已经存在的所有这些重载

现在看来,就像调用setValuesomePlug一样,false是模糊的。然而,布尔过载更专业,因此将根据C++规则…

< p>更好:除了回答:它甚至可能有三个重载,一个接受布尔,STD::String和一个通用的。在这个非常具体的用例中,它可能没有意义,但您需要自己决定,但为了完整性,请将其视为更一般的扩展,我仍将展示:

class CommandInterfaceFont
{
public:

    template <typename plug , typename typeValue >
    void setValue(plug* geom, std::string stdstrParamName, typeValue value)
    {
        // does something generic, whatever this would be
    }

    template <typename plug >
    void setValue(plug* geom, std::string stdstrParamName, std::string value)
    {
        // specific
    }

    template <typename plug >
    void setValue(plug* geom, std::string stdstrParamName, bool value)
    {
        // specific
    }
};
要做到这一点,关键在于您至少需要声明类范围中已经存在的所有这些重载


现在看来,就像调用setValuesomePlug一样,false是模糊的。然而,布尔过载更专业,因此将根据C++规则……< /P>优先。不允许对functionmethod进行部分专门化。虽然部分专门化的语法是错误的。。。如果你真的想有重载,你需要在类中声明它们,而你没有。你没有修改字符串,是吗?因此,您可能更愿意接受它们作为常量引用,以避免不必要的副本。您是否考虑过通过引用而不是指针接受插件参数?您不需要为空指针操心,也不需要检查,因此如果严格,您实际上必须记录传递空指针是无效的/调用未定义的行为。您编写的不是重载,而是部分专门化。不允许对functionmethod进行部分专门化。虽然部分专门化的语法是错误的。。。如果你真的想有重载,你需要在类中声明它们,而你没有。你没有修改字符串,是吗?因此,您可能更愿意接受它们作为常量引用,以避免不必要的副本。您是否考虑过通过引用而不是指针接受插件参数?您不需要为空指针操心,也不需要检查,因此如果严格要求,实际上,您必须记录传递空指针是无效的/调用未定义的行为。我建议在声明周围添加类上下文,以使它们实际需要出现的位置更加明确。我建议在声明周围添加类上下文,以使它们实际需要出现的位置更加明确。