-1

I have a method which can receive datatype of template class type. In that function, I have to do the further processing if the datatype of the arguments is not of string type. And, if the arguments are of string type then, I want to do the exception handling.

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;
}
user2756695
  • 676
  • 1
  • 7
  • 22
  • @CoryKramer this question is about a class's template parameter – Ryan Haining Sep 25 '15 at 22:26
  • @RyanHaining I know, the *exact* same principal applies whether this is a template class or template function. `std::is_same::value` – Cory Kramer Sep 25 '15 at 22:27
  • @CoryKramer using `std::is_same` in both, yes, but not the way to apply it. The answers on the marked duplicate include function specializations, OP would be better off *not* specializing the entire class for this purpose, and you can't overload a class in the same way. – Ryan Haining Sep 25 '15 at 22:29
  • If you feel that the OP's question is fundamentally different than the duplicate, then feel free to vote to reopen it. I feel that it has the same underlying question, and what they choose to do after that point is slightly different, so I will not vote to reopen the question myself. – Cory Kramer Sep 25 '15 at 22:30

1 Answers1

0

What about using std::is_same:

if (std::is_same<K, string>::value) {
    // K is string ...
}
Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • Thanks, I want to apply exception handling here. Could you please guide about it? – user2756695 Sep 25 '15 at 22:26
  • Why the downvote? This exactly answers the OP's question. (Whether it's the best way to do what he may want to do is another matter.) – Claudiu Sep 25 '15 at 23:34