Template Specialization
Completed
Write template <typename T> string describe(T x) that returns value: X, and a full specialization for bool returning flag: yes|no. Read an int, a double and a bool (0/1) and describe each.
Input
One line: int double bool.
Output
Three lines.
Example
Input
42 3.5 1
Output
value: 42 value: 3.5 flag: yes
Editor
C++
Hints
template <> string describe<bool>(bool x) replaces the general version for bool.
Solution explanation
Specialization customises a template for one particular type.
Show solution
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
template <typename T>
string describe(T x) {
ostringstream out;
out << "value: " << x;
return out.str();
}
template <>
string describe<bool>(bool x) {
return string("flag: ") + (x ? "yes" : "no");
}
int main() {
int i;
double d;
int b;
cin >> i >> d >> b;
cout << describe(i) << "\n" << describe(d) << "\n" << describe(b != 0) << "\n";
return 0;
}