CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #98

A Default Type Argument Fills the Gap

mediumTemplates

📄 Code

Read carefully — what does this print?
1#include <iostream>
2 
3template <typename T, typename U = T>
4class Range {
5 T m_low;
6 U m_high;
7public:
8 Range(T low, U high) : m_low(low), m_high(high) {}
9 U high() const { return m_high; }
10 void print() const {
11 std::cout << m_low << ".." << m_high << "\n";
12 }
13};
14 
15int main() {
16 Range<int> a(1, 9);
17 Range<int, double> b(1, 9.5);
18 Range<int> c(1, 9.5);
19 Range<double> d(0.5, 2.5);
20 a.print();
21 b.print();
22 c.print();
23 d.print();
24 std::cout << b.high() * 2 << " " << c.high() * 2 << "\n";
25 return 0;
26}

🎯 Your Answer