user877329
user877329

Reputation: 6230

Template specialization of template class member function

I am trying to specialize template class member functions:

In 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;
        };
    }

In 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"));
    }

Compiler output:

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

valueinput.cpp:21:45: error: specialization of 'void Gui::ValueInput::displayUpdate() [with T = double]' in different namespace [-fpermissive]

valueinput.cpp:21:6: error: from definition of 'void Gui::ValueInput::displayUpdate() [with T = double]' [-fpermissive]

valueinput.cpp:27:43: error: specialization of 'void Gui::ValueInput::valueUpdate() [with T = double]' in different namespace [-fpermissive]

valueinput.cpp:27:6: error: from definition of 'void Gui::ValueInput::valueUpdate() [with T = double]' [-fpermissive]

What is wrong?

Upvotes: 0

Views: 1578

Answers (1)

Constructor
Constructor

Reputation: 7493

Rewrite the code in your valueinput.cpp file in the following manner:

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"));
    }
}

And don't forget to declare these specializations in valueinput.h header file if you want to use them outside the valueinput.cpp file:

namespace Gui
{
    template<class T>
    class ValueInput : public TextInput
    {
        // ...
    };

    template<>
    void ValueInput<double>::displayUpdate();

    template<>
    void ValueInput<double>::valueUpdate();

}

Edit: I don't know is your variant standard-compliant or not. But here is a small quotation from the standard ([temp.expl.spec] 14.7.3/8):

A template explicit specialization is in the scope of the namespace in which the template was defined. [ Example:

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

— end example ]

Unfortunately it is about class template specializations, not about function template specializations or function member specializations of class templates.

Upvotes: 1

Related Questions