Rya*_*yan 27 c++ gcc templates
我试图移植以下代码.我知道标准不允许在非namescape范围内进行显式特化,我应该使用重载,但我找不到在这种特殊情况下应用这种技术的方法.
class VarData
{
public:
template < typename T > bool IsTypeOf (int index) const
{
return IsTypeOf_f<T>::IsTypeOf(this, index); // no error...
}
template <> bool IsTypeOf < int > (int index) const // error: explicit specialization in non-namespace scope 'class StateData'
{
return false;
}
template <> bool IsTypeOf < double > (int index) const // error: explicit specialization in non-namespace scope 'class StateData'
{
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
CB *_*ley 41
您只需将成员模板的专业化移到类主体之外.
class VarData
{
public:
template < typename T > bool IsTypeOf (int index) const
{
return IsTypeOf_f<T>::IsTypeOf(this, index);
}
};
template <> bool VarData::IsTypeOf < int > (int index) const
{
return false;
}
template <> bool VarData::IsTypeOf < double > (int index) const
{
return false;
}
Run Code Online (Sandbox Code Playgroud)
将类外的特化定义为:
template <>
bool VarData::IsTypeOf < int > (int index) const
{ //^^^^^^^^^ don't forget this!
return false;
}
template <>
bool VarData::IsTypeOf < double > (int index) const
{ //^^^^^^^ don't forget this!
return false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20132 次 |
| 最近记录: |