[英]Template specialization of template class member function
I am trying to specialize template class member functions: 我正在尝试专门化模板类成员函数:
In valueinput.h 在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 在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:45:错误:在不同的命名空间[-fpermissive]中“ void Gui :: ValueInput :: displayUpdate()[with T = double]”的专业化
valueinput.cpp:21:6: error: from definition of 'void Gui::ValueInput::displayUpdate() [with T = double]' [-fpermissive]
valueinput.cpp:21:6:错误:来自'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:43:错误:“ void Gui :: ValueInput :: valueUpdate()[with T = double]”在不同名称空间中的特化[-fpermissive]
valueinput.cpp:27:6: error: from definition of 'void Gui::ValueInput::valueUpdate() [with T = double]' [-fpermissive]
valueinput.cpp:27:6:错误:来自'void Gui :: ValueInput :: valueUpdate()[with T = double]'的定义[-fpermissive]
What is wrong? 怎么了?
Rewrite the code in your valueinput.cpp
file in the following manner: 通过以下方式将代码重写在
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"));
}
}
And don't forget to declare these specializations in valueinput.h
header file if you want to use them outside the valueinput.cpp
file: 而且,如果要在
valueinput.cpp
文件之外使用它们,请不要忘记在valueinput.h
头文件中声明这些专业化:
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):
但这是标准的一小部分报价([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. 不幸的是,它与类模板专业化有关,而与类模板的功能模板专业化或功能成员专业化无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.