C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
exercise-2-4.hpp
Go to the documentation of this file.
1// ===-- chapter-2/exercise-2-4.hpp ----------------------- -*- C++ -*- --=== //
9#ifndef EXERCISE_2_4
10#define EXERCISE_2_4
11
12#include <iostream>
13
14
30namespace exercise_2_4 {
31
50template <typename T> struct type_descriptor { };
51
53// Non-leaf data-types:
55
56template <typename T>
57std::ostream& operator<<(std::ostream& os, type_descriptor<T const> const&)
58{
59 return os << type_descriptor<T>() << " const";
60}
61template <typename T>
62std::ostream& operator<<(std::ostream& os, type_descriptor<T&> const&)
63{
64 return os << type_descriptor<T>() << '&';
65}
66template <typename T>
67std::ostream& operator<<(std::ostream& os, type_descriptor<T*> const&)
68{
69 return os << type_descriptor<T>() << '*';
70}
71template <typename T, size_t N>
72std::ostream& operator<<(std::ostream& os, type_descriptor<T[N]> const&)
73{
74 return os << type_descriptor<T>() << '[' << N << ']';
75}
76template <typename T>
77std::ostream& operator<<(std::ostream& os, type_descriptor<T()> const&)
78{
79 return os << type_descriptor<T>() << "()";
80}
81template <typename T>
82std::ostream& operator<<(std::ostream& os, type_descriptor<T(*)()> const&)
83{
84 return os << type_descriptor<T>() << "(*)()";
85}
86
88// Leaf data-types:
90
91std::ostream& operator<<(std::ostream& os, type_descriptor<char> const&)
92{ return os << "char"; }
93std::ostream& operator<<(std::ostream& os, type_descriptor<short> const&)
94{ return os << "short int"; }
95std::ostream& operator<<(std::ostream& os, type_descriptor<int> const&)
96{ return os << "int"; }
97std::ostream& operator<<(std::ostream& os, type_descriptor<long> const&)
98{ return os << "long int"; }
99
100} // namespace exercise_2_4
101
102#endif // EXERCISE_2_4
std::ostream & operator<<(std::ostream &os, type_descriptor< T > const &)
Encapsulate solution for Exercise 2-4.
Print (a limited set of) data-types.