Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
template <typename T> struct Tag { char get() { return 'T'; } };
template <> struct Tag<int> { char get() { return 'i'; } };
int main() {
Tag<int> a;
Tag<short> b;
std::cout << a.get() << b.get();
}iT
- Expecting
ii: a specialization serves only its exact type.shortis not promoted tointwhen a class is chosen, soTag<short>comes from the primary template. - Expecting
TT:Tag<int>has a full specialization, and it replaces the primary template forint.
- How would you give
Taga separate version fordouble? - Why can a full specialization have members that the primary template lacks?