有幾個問題已經與堆棧溢出相似,但似乎沒有任何東西可以直接回答我的問題。如果我正在轉貼,我很抱歉。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