模板类成员函数的模板特化
Template specialization of template class member function
我正在尝试特化模板类成员函数:
在valueinput.hnamespace 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
不幸的是,它是关于类模板特化的,而不是关于函数模板特化或类模板的函数成员特化的。
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法