vinothrajendran4
vinothrajendran4

Reputation: 141

same Template function for int and std::string

Is there a way to use same template function for performing operation on int and std::string, eg:

template<typename T>
T add(typename a, typename b)
{
   return (a+b);       // incase for int
   return a.append(b); // incase for std::string
}

Any help is appreciated.

Upvotes: 0

Views: 114

Answers (1)

Ben Crowhurst
Ben Crowhurst

Reputation: 8490

std::string supports the operator+.

concatenates two strings or a string and a char

template<typemane T>
T add(typename a, typename b)
{
   return a + b;
}

Upvotes: 1

Related Questions