有几个问题已经与堆栈溢出相似,但似乎没有任何东西可以直接回答我的问题。如果我正在转贴,我很抱歉。C++ - 使用该方法的部分特化的超载模板化类方法
我想重载模板类(有2个模板参数)的几个方法与这些方法的部分模板专业化。我一直无法弄清楚正确的语法,并开始认为这是不可能的。我想我会在这里张贴,看看我能否得到确认。
示例代码遵循:
template <typename T, typename U>
class Test
{
public:
void Set(T t, U u);
T m_T;
U m_U;
};
// Fully templated method that should be used most of the time
template <typename T, typename U>
inline void Test<T,U>::Set(T t, U u)
{
m_T=t;
m_U=u;
}
// Partial specialisation that should only be used when U is a float.
// This generates compile errors
template <typename T>
inline void Test<T,float>::Set(T t, float u)
{
m_T=t;
m_U=u+0.5f;
}
int _tmain(int argc, _TCHAR* argv[])
{
Test<int, int> testOne;
int a = 1;
testOne.Set(a, a);
Test<int, float> testTwo;
float f = 1.f;
testTwo.Set(a, f);
}
我知道我可以写整个班级的部分专业化,但还挺吮吸。是这样的可能吗?
(我使用VS2008) 编辑:这是编译错误 错误C2244: '测试::设置':无法定义的功能匹配到一个现有的声明
谢谢:)
这是一个好主意。谢谢 :) – JBeFat 2011-03-05 20:51:58