type_traits.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * cxxomfort example: type_traits
  3. *
  4. * This program provides sample usage of the various
  5. * type_traits in C++, backported to the various revisions.
  6. */
  7. #include <cxxomfort/cxxomfort.hpp>
  8. #include <cxxomfort/library/type_name.hpp>
  9. #include <cxxomfort/library/type_traits.hpp>
  10. #include <cxxomfort/type_traits.hpp>
  11. #include <cxxomfort/impl/11-is_constructible.hpp>
  12. #include <type_traits>
  13. #include <array>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <string>
  17. #include <deque>
  18. #include <utility>
  19. struct Foo {
  20. Foo (int, char) CXXO_NOEXCEPTNOTHROW {}
  21. Foo (int, wchar_t) {}
  22. template <typename T>
  23. explicit Foo (T) {}
  24. CXXO_CONSTEXPR Foo (Foo const&) CXXO_NOEXCEPTNOTHROW CXXOMFORT_CXX11_CODE(=default, {});
  25. virtual ~Foo() {}
  26. private:
  27. };
  28. typedef float T1;
  29. typedef enum { value = 1 } T2;
  30. //typedef std::ios_base::fmtflags T2;
  31. typedef Foo T3;
  32. template <typename T>
  33. std::ostream& basic_traits(std::ostream& os) ;
  34. template <typename T>
  35. void cv_traits(std::ostream& os) ;
  36. template <typename T>
  37. void property_traits(std::ostream& os) ;
  38. //template <typename T>
  39. //void construction_traits(std::ostream& os) ;
  40. //template <typename T>
  41. //void supplemental_traits(std::ostream& os) ;
  42. int main () {
  43. using namespace std;
  44. using cxxomfort::library::type_name;
  45. cxxomfort::output_info();
  46. cout<< endl;
  47. cout<< "T1 is: "<< type_name<T1>()<< endl;
  48. cout<< "T2 is: "<< type_name<T2>()<< endl;
  49. cout<< "T3 is: "<< type_name<T3>()<< endl;
  50. cout<< boolalpha<< endl;
  51. basic_traits<int>(cout);
  52. property_traits<int>(cout);
  53. //construction_traits<int>(cout);
  54. //supplemental_traits<int>(cout);
  55. cout<< "----\n"<< endl;
  56. // fill in a type here
  57. typedef pair<const int, const bool> TT ;
  58. //
  59. basic_traits<TT>(cout);
  60. cv_traits<TT>(cout);
  61. property_traits<TT>(cout);
  62. //construction_traits<TT>(cout);
  63. //supplemental_traits<TT>(cout);
  64. cout<< "----\n"<< endl;
  65. cout<< "Info for Foo: "<< endl;
  66. basic_traits<Foo>(cout);
  67. property_traits<Foo>(cout);
  68. //construction_traits<Foo>(cout);
  69. //supplemental_traits<Foo>(cout);
  70. cout<< endl;
  71. cout<< endl;
  72. #if 0
  73. cout<< type_name< CXXO_TYPEOF(declval<void>()) >()<< endl;
  74. cout<< type_name< CXXO_TYPEOF(declval<int>()) >()<< endl;
  75. #if (CXXOMFORT_CXX_STD >= 2011)
  76. using ::cxxomfort::library::type_traits::is_specialization_of;
  77. typedef pair<string, bool> P;
  78. cout<< "pair ";
  79. cout<< is_specialization_of< P, pair >::value<< endl;
  80. cout<< is_specialization_of< string , pair >::value<< endl;
  81. cout<< "string ";
  82. cout<< is_specialization_of< P, basic_string >::value<< endl;
  83. cout<< is_specialization_of< string, basic_string>::value<< endl;
  84. #else
  85. using ::cxxomfort::library::type_traits::detail::is_specialization_of_2;
  86. using ::cxxomfort::library::type_traits::detail::is_specialization_of_3;
  87. typedef pair<string, bool> P;
  88. cout<< "pair ";
  89. cout<< is_specialization_of_2< P, pair >::value<< endl;
  90. cout<< is_specialization_of_2< string , pair >::value<< endl;
  91. cout<< "string ";
  92. cout<< is_specialization_of_3< P, basic_string >::value<< endl; //< this one fails because P is smaller than basic_string
  93. cout<< is_specialization_of_3< string, basic_string>::value<< endl;
  94. #endif
  95. #endif
  96. }
  97. template <typename T>
  98. std::ostream& basic_traits(std::ostream& os) {
  99. using namespace std;
  100. using cxxomfort::library::type_name;
  101. using cxxomfort::library::typeid_demangle;
  102. os<< "T : "<< type_name<T>()<< " is:"<< endl;
  103. os<< ""<< (is_integral<T>::value ? "integral\t" : "");
  104. os<< ""<< (is_floating_point<T>::value ? "floating\t" : "");
  105. os<< ""<< (is_arithmetic<T>::value ? "arithmetic\t" : "");
  106. os<< ""<< (is_null_pointer<T>::value ? "nullptr_t\t" : "");
  107. os<< ""<< (is_void<T>::value ? "void\t" : "");
  108. os<< " ; ";
  109. os<< ""<< (is_pointer<T>::value ? "pointer " : "");
  110. os<< ""<< (is_member_pointer<T>::value ? "member-pointer " : "");
  111. os<< ""<< (is_union<T>::value ? "union " : "");
  112. os<< ""<< (is_enum<T>::value ? "enum " : "");
  113. os<< ""<< (is_array<T>::value ? "array " : "");
  114. os<< ""<< (is_class<T>::value ? "class " : "");
  115. os<< " ; ";
  116. os<< ""<< (is_scalar<T>::value ? "scalar " : "");
  117. os<< ""<< (is_object<T>::value ? "object " : "");
  118. os<< ""<< (is_lvalue_reference<T>::value ? "lvalue-ref " : "");
  119. os<< ""<< (is_rvalue_reference<T>::value ? "rvalue-ref " : "");
  120. os<< ""<< (is_reference<T>::value ? "reference " : "");
  121. os<< ""<< (is_function<T>::value ? "function " : "");
  122. os<< " ; ";
  123. os<< ""<< (is_fundamental<T>::value ? "fundamental " : "");
  124. os<< ""<< (is_compound<T>::value ? "compound " : "");
  125. os<< endl;
  126. return os;
  127. }
  128. template <typename T>
  129. void cv_traits(std::ostream& os) {
  130. using namespace std;
  131. using cxxomfort::library::type_name;
  132. os<< "T : cv-Qualifications: ";
  133. os<< ""<< (is_const<T>::value ? "const " : "");
  134. os<< ""<< (is_volatile<T>::value ? "volatile " : "");
  135. os<< endl;
  136. return; // os<< endl;
  137. }
  138. template <typename T>
  139. void property_traits(std::ostream& os) {
  140. using std::endl;
  141. using namespace std;
  142. using cxxomfort::library::type_name;
  143. os<< "T : Type Properties: "<< endl;
  144. os<< "decayed form: "<< cxxomfort::library::typeid_demangle( typeid(typename decay<T>::type) )<< flush<< '\n';
  145. os<< ""<< (is_empty<T>::value ? "empty " : "");
  146. os<< ""<< (is_trivial<T>::value ? "trivial " : "");
  147. os<< ""<< (is_pod<T>::value ? "pod " : "");
  148. os<< ""<< (is_literal_type<T>::value ? "literal-type " : "");
  149. os<< ""<< (is_standard_layout<T>::value ? "standard-layout " : "");
  150. os<< " ; ";
  151. os<< ""<< (is_polymorphic<T>::value ? "polymorphic " : "");
  152. os<< ""<< (is_abstract<T>::value ? "abstract " : "");
  153. os<< ""<< (is_final<T>::value ? "final " : "");
  154. os<< endl;
  155. return; //os<< endl;
  156. }
  157. #if 0
  158. template <typename T>
  159. void construction_traits(std::ostream& os) {
  160. using namespace std;
  161. using cxxomfort::library::type_name;
  162. os<< "T : Construction: "<< endl;
  163. os<< " Method | triv | nothr | constexpr | member |\n"
  164. << " ---------------+--------+--------+-----------+--------+ "<< endl;
  165. os<< " def-ctor | ";
  166. os<< setw(6)<< setfill('_')<< right<< (is_trivially_default_constructible<T>::value)<< " ";
  167. os<< setw(6)<< setfill('_')<< right<< (is_nothrow_default_constructible<T>::value)<< " ";
  168. os<< setw(9)<< setfill('_')<< right<< false<< " ";
  169. os<< setw(6)<< setfill('_')<< right<< (is_default_constructible<T>::value)<< " ";
  170. os<< endl;
  171. os<< " copy-ctor | ";
  172. os<< setw(6)<< setfill('_')<< right<< (is_trivially_copy_constructible<T>::value)<< " ";
  173. os<< setw(6)<< setfill('_')<< right<< (is_nothrow_copy_constructible<T>::value)<< " ";
  174. os<< setw(9)<< setfill('_')<< right<< false<< " ";
  175. os<< setw(6)<< setfill('_')<< right<< (is_copy_constructible<T>::value)<< " ";
  176. os<< endl;
  177. os<< " move-ctor | ";
  178. os<< setw(6)<< setfill('_')<< right<< (is_trivially_move_constructible<T>::value)<< " ";
  179. os<< setw(6)<< setfill('_')<< right<< (is_nothrow_move_constructible<T>::value)<< " ";
  180. os<< setw(9)<< setfill('_')<< right<< false<< " ";
  181. os<< setw(6)<< setfill('_')<< right<< (is_move_constructible<T>::value)<< " ";
  182. os<< endl;
  183. os<< " destructor | ";
  184. os<< setw(6)<< setfill('_')<< right<< (is_trivially_destructible<T>::value)<< " ";
  185. os<< setw(6)<< setfill('_')<< right<< (is_nothrow_destructible<T>::value)<< " ";
  186. os<< setw(9)<< setfill('_')<< right<< false<< " ";
  187. os<< setw(6)<< setfill('_')<< right<< (is_destructible<T>::value)<< " ";
  188. //os<< (has_virtual_destructor<T>::value ? "~virtual ":"");
  189. os<< endl;
  190. os<< " ---------------+--------+--------+-----------+--------+ "<< endl;
  191. (void)os;
  192. }
  193. #endif // 0
  194. #if 0
  195. template <typename T>
  196. void supplemental_traits(std::ostream& os) {
  197. using std::endl;
  198. using namespace std;
  199. using namespace cxxomfort::library::type_traits;
  200. os<< "T : Supplemental traits from cxxomfort: "<< endl;
  201. os<< ""<< (is_character_type<T>::value ? "character-type " : "");
  202. os<< ""<< (is_std_byte<T>::value ? "std::byte " : "");
  203. os<< ""<< (is_std_array<T>::value ? "std::array " : "");
  204. os<< ""<< (is_std_pair<T>::value ? "std::pair " : "");
  205. os<< endl;
  206. return; //os<< endl;
  207. }
  208. #endif