C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
exercise-2-5.hpp
Go to the documentation of this file.
1// ===-- chapter-2/exercise-2-5.hpp ----------------------- -*- C++ -*- --=== //
9#ifndef EXERCISE_2_5
10#define EXERCISE_2_5
11
12#include <iostream>
13
14
35namespace exercise_2_5 {
36
41template <typename T> struct type_descriptor_eng { };
42
44// Non-leaf data-types:
46
47template <typename T>
48std::ostream& operator<<(std::ostream& os, type_descriptor_eng<T const> const&)
49{
50 return os << "const " << type_descriptor_eng<T>();
51}
52template <typename T>
53std::ostream& operator<<(std::ostream& os, type_descriptor_eng<T&> const&)
54{
55 return os << "reference to " << type_descriptor_eng<T>();
56}
57template <typename T>
58std::ostream& operator<<(std::ostream& os, type_descriptor_eng<T*> const&)
59{
60 return os << "pointer to " << type_descriptor_eng<T>();
61}
62template <typename T>
63std::ostream& operator<<(std::ostream& os, type_descriptor_eng<T[]> const&)
64{
65 return os << "array of " << type_descriptor_eng<T>();
66}
67template <typename T, size_t N>
68std::ostream& operator<<(std::ostream& os, type_descriptor_eng<T[N]> const&)
69{
70 return os << N << " element array of " << type_descriptor_eng<T>();
71}
72template <typename T>
73std::ostream& operator<<(std::ostream& os, type_descriptor_eng<T()> const&)
74{
75 return os << "function returning " << type_descriptor_eng<T>();
76}
77template <typename T>
78std::ostream& operator<<(std::ostream& os, type_descriptor_eng<T(*)()> const&)
79{
80 return os << "pointer to function returning " << type_descriptor_eng<T>();
81}
82
84// Leaf data-types:
86
87std::ostream& operator<<(std::ostream& os, type_descriptor_eng<char> const&)
88{ return os << "char"; }
89std::ostream& operator<<(std::ostream& os, type_descriptor_eng<short> const&)
90{ return os << "short int"; }
91std::ostream& operator<<(std::ostream& os, type_descriptor_eng<int> const&)
92{ return os << "int"; }
93std::ostream& operator<<(std::ostream& os, type_descriptor_eng<long> const&)
94{ return os << "long int"; }
95
96} // namespace exercise_2_5
97
98#endif // EXERCISE_2_5
std::ostream & operator<<(std::ostream &os, type_descriptor_eng< T > const &)
Encapsulate solution for Exercise 2-5.
Display data-types in English wording.