模板类成员函数的模板特化

Template specialization of template class member function

本文关键字:成员 函数      更新时间:2023-10-16

我正在尝试特化模板类成员函数:

在valueinput.h

namespace Gui
    {
    template<class T>
    class ValueInput:public TextInput
        {
        public:         
            static ValueInput* create(Gui& gui_obj,uint32_t style_0,uint32_t style_1
                ,Window* parent,T& obj)
                {return new ValueInput(gui_obj,style_0,style_1,parent,obj);}
            //Polymorphic implementation inherited from
            //TextInput that needs specialization depending on T
            void valueUpdate();
            //Polymorphic implementation inherited from
            //TextInput that needs specialization depending on T
            void displayUpdate();
        protected:
            ValueInput(Gui& gui_obj,uint32_t style_0,uint32_t style_1,Window* parent
                ,T& obj):TextInput(gui_obj,style_0,style_1,parent),ptr_obj(&obj)
                {}
        private:
            T* ptr_obj;
        };
    }
在valueinput.cpp

template<>
void Gui::ValueInput<double>::displayUpdate()   
    {
    Dialog::messageDisplay(this,{STR("Display Update"),Herbs::LogMessage::Type::INFORMATION},STR("Test"));
    }
template<>
void Gui::ValueInput<double>::valueUpdate() 
    {
    Dialog::messageDisplay(this,{STR("Value Update"),Herbs::LogMessage::Type::INFORMATION},STR("Test"));
    }
编译器输出:

g++ "valueinput.cpp" -g -municode -Wall -c -std=c++11 -o "__wand_targets_dbgvalueinput.o"

ValueInput .cpp:21:45:错误:特化'void Gui::ValueInput::displayUpdate() [with T = double]'在不同的命名空间[-fpermissive]

ValueInput .cpp:21:6:错误:从定义'void Gui::ValueInput::displayUpdate() [with T = double]' [-fpermissive]

ValueInput .cpp:27:43:错误:特化'void Gui::ValueInput::valueUpdate() [with T = double]'在不同的命名空间[-fpermissive]

ValueInput .cpp:27:6:错误:从定义'void Gui::ValueInput::valueUpdate() [with T = double]' [-fpermissive]

怎么了?

按照以下方式重写valueinput.cpp文件中的代码:

namespace Gui
{
    template<>
    void ValueInput<double>::displayUpdate()   
    {
        Dialog::messageDisplay(this,{STR("Display Update"),Herbs::LogMessage::Type::INFORMATION},STR("Test"));
    }
    template<>
    void ValueInput<double>::valueUpdate() 
    {
        Dialog::messageDisplay(this,{STR("Value Update"),Herbs::LogMessage::Type::INFORMATION},STR("Test"));
    }
}

如果你想在valueinput.cpp文件之外使用它们,不要忘记在valueinput.h头文件中声明这些专门化:

namespace Gui
{
    template<class T>
    class ValueInput : public TextInput
    {
        // ...
    };
    template<>
    void ValueInput<double>::displayUpdate();
    template<>
    void ValueInput<double>::valueUpdate();
}

编辑:我不知道你的变体是否符合标准。但这里有一段来自标准([temp.expl])的小引言。规范]14.7.3/8):

模板显式专门化在命名空间的范围内定义了哪个模板。(例子:

namespace N {
  template<class T> class X { /* ... */ };
  template<class T> class Y { /* ... */ };
  template<> class X<int> { /* ... */ };      // OK: specialization
                                              // in same namespace
  template<> class Y<double>;                 // forward declare intent to
                                              // specialize for double
}
template<> class N::Y<double> { /* ... */ };  // OK: specialization
                                              // in same namespace

不幸的是,它是关于类模板特化的,而不是关于函数模板特化或类模板的函数成员特化的。