175. Template

Question.4

A generic add function needs special handling for chars:

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

template<>
char add<char>(char a, char b) {
   int r = a + b;
   return (r > 127) ? 127 : (char)r; // Saturate
}

printf("%d", add<char>(100, 50));

What is printed?

Need Help? Refer to the Quick Guide below

Select Answer