我有一種方法可以接收模板類類型的數據類型。在那個函數中,如果參數的數據類型不是字符串類型,我必須做進一步的處理。而且,如果參數是字符串類型,那麼我想要執行異常處理。如果傳遞給函數的參數的數據類型是字符串類型,則拋出異常
template<class K>
class Student
{
private:
K array[10];
public:
void assignvalues(K const& index, const K& val)
{
//NOW, I want to check here that the index is not of string type
array[index] = val;
}
};
int main()
{
Student <int> object;
object.assignvalues(5,9);
//BUT THIS WILL NOT WORK
Student <string> object;
object.assignvalues("Hi","value");
return 0;
}
@CoryKramer這個問題是關於一個類的模板參數 –
@RyanHaining我知道,*確切*相同的原則是否適用,這是一個模板類或模板函數。 'std :: is_same :: value' –
CoryKramer
@CoryKramer在兩者中都使用'std :: is_same',是的,但不是應用它的方式。標記爲重複的答案包括函數特化,OP會更好*不專門爲此目的整個類,並且不能以相同方式重載類。 –