0sd6.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Simple test file for cxxomfort
  3. * When compiled and run, this will print information about the
  4. * C++ and cxxomfort install, and what SD6 / feature macros have been detected or defined by the compiler or by the library.
  5. *
  6. */
  7. #include <cxxomfort/base.hpp>
  8. #include <cxxomfort/backports.hpp>
  9. #include <cxxomfort/library/type_name.hpp>
  10. #include <list>
  11. #include <iostream>
  12. #include <cstdio> // for snprintf
  13. /*
  14. * BEGIN SECTION
  15. * Uncomment these as desired, if you have the required cxxomfort extensions
  16. * Alternatively, add here any third party that provides or facades the functionality and announces it as defined by the C++ Standard (ie.: via the required __cpp_lib_ macros).
  17. *
  18. * */
  19. // from cxxomfort:vocab17
  20. //#include <cxxomfort/any.hpp>
  21. //#include <cxxomfort/optional.hpp>
  22. //#include <cxxomfort/variant.hpp>
  23. // from cxxomfort:minranges
  24. //#include <cxxomfort/ranges.hpp>
  25. /*
  26. * END SECTION
  27. * */
  28. #if (CXXOMFORT_CXX_STD>=2020)
  29. #include <version>
  30. #endif
  31. #define CCHECK_HELPER(...) (#__VA_ARGS__[0] == '1' || #__VA_ARGS__[0] == '\0')
  32. #define CCHECK(...) CCHECK_HELPER(__VA_ARGS__)
  33. #define CXXOSHOWSUPPORT(macro) (CXXO_IF___cpp_feature(macro) ? ("" #macro " : " CXXO_EXPAND(CXXO_STRINGIZE(macro) ) "\n") : "")
  34. void show_sd6_feature (std::ostream&);
  35. void show_sd6_library (std::ostream&);
  36. std::array<int,5> arr = {{5, 4, 3, 2, 1}};
  37. std::list<int> lst;
  38. int main () {
  39. using std::cout;
  40. using std::endl;
  41. // for container related tests
  42. (void)arr;
  43. (void)lst;
  44. cxxomfort::output_info(stdout);
  45. cout<< endl;
  46. cout<< "SD6 macros:\n";
  47. cout<< "C++ Compiler Feature:\n";
  48. show_sd6_feature(cout);
  49. cout<< "C++ Library Feature:\n";
  50. show_sd6_library(cout);
  51. cout<< "\n"<< endl;
  52. cout<< "\nC++20 lib:"<< endl;
  53. cout<< CXXOSHOWSUPPORT(__cpp_lib_atomic_ref);
  54. cout<< CXXOSHOWSUPPORT(__cpp_lib_bind_front);
  55. cout<< CXXOSHOWSUPPORT(__cpp_lib_bit_cast);
  56. cout<< CXXOSHOWSUPPORT(__cpp_lib_bitops);
  57. #if (defined(__cpp_lib_bitops))
  58. {
  59. cout<< "test bit_floor, bit_ceil: "<< std::flush;
  60. uint32_t const x = 0x00123400;
  61. cout<< x<< " \t"<< std::bit_floor(x)<< " "<< std::bit_ceil(x)<< endl;
  62. }
  63. #endif
  64. cout<< CXXOSHOWSUPPORT(__cpp_lib_bounded_array_traits);
  65. cout<< CXXOSHOWSUPPORT(__cpp_lib_constexpr_algorithms);
  66. cout<< CXXOSHOWSUPPORT(__cpp_lib_constexpr_numeric);
  67. cout<< CXXOSHOWSUPPORT(__cpp_lib_constexpr_string_view);
  68. cout<< CXXOSHOWSUPPORT(__cpp_lib_destroying_delete);
  69. cout<< CXXOSHOWSUPPORT(__cpp_lib_endian);
  70. cout<< CXXOSHOWSUPPORT(__cpp_lib_erase_if);
  71. cout<< CXXOSHOWSUPPORT(__cpp_lib_int_pow2);
  72. cout<< CXXOSHOWSUPPORT(__cpp_lib_integer_comparison_functions);
  73. cout<< CXXOSHOWSUPPORT(__cpp_lib_interpolate);
  74. #if (defined(__cpp_lib_interpolate))
  75. {
  76. cout<< "Test midpoint: "<< std::midpoint(SHRT_MIN, SHRT_MAX)<< endl;
  77. }
  78. #endif
  79. cout<< CXXOSHOWSUPPORT(__cpp_lib_remove_cvref);
  80. cout<< CXXOSHOWSUPPORT(__cpp_lib_shift);
  81. #if (defined(__cpp_lib_shift))
  82. {
  83. cout<< "test on shift:"<< endl;
  84. std::array<int,5> arr2= arr;
  85. CXXO_FOREACH(int a, arr2) { cout<< a<< "\t"; } cout<< endl;
  86. std::shift_right(arr2.begin(), arr2.end(), 2);
  87. CXXO_FOREACH(int a, arr2) { cout<< a<< "\t"; } cout<< endl;
  88. std::shift_left(arr2.begin(), arr2.end(), 2);
  89. CXXO_FOREACH(int a, arr2) { cout<< a<< "\t"; } cout<< endl;
  90. }
  91. #endif
  92. cout<< CXXOSHOWSUPPORT(__cpp_lib_span);
  93. cout<< CXXOSHOWSUPPORT(__cpp_lib_ssize);
  94. #if (defined(__cpp_lib_ssize))
  95. {
  96. cout<< "test on array: "<< std::ssize(arr)<< endl;
  97. }
  98. #endif
  99. cout<< CXXOSHOWSUPPORT(__cpp_lib_starts_ends_with);
  100. cout<< CXXOSHOWSUPPORT(__cpp_lib_to_array);
  101. cout<< CXXOSHOWSUPPORT(__cpp_lib_type_identity);
  102. cout<< CXXOSHOWSUPPORT(__cpp_lib_unwrap_ref);
  103. cout<< "\nC++23 lib:"<< endl;
  104. cout<< CXXOSHOWSUPPORT(__cpp_lib_bind_back);
  105. cout<< CXXOSHOWSUPPORT(__cpp_lib_byteswap);
  106. cout<< CXXOSHOWSUPPORT(__cpp_lib_expected);
  107. cout<< CXXOSHOWSUPPORT(__cpp_lib_invoke_r);
  108. #if (defined(__cpp_lib_invoke_r))
  109. {
  110. int test = std::invoke_r<int>(std::snprintf, nullptr, 0, "Number %03d.", 5);
  111. cout<< "test of invoke_r: "<< test<< " characters needed."<< endl;
  112. }
  113. #endif
  114. cout<< CXXOSHOWSUPPORT(__cpp_lib_is_scoped_enum);
  115. cout<< CXXOSHOWSUPPORT(__cpp_lib_string_contains);
  116. cout<< CXXOSHOWSUPPORT(__cpp_lib_to_underlying);
  117. cout<< CXXOSHOWSUPPORT(__cpp_lib_unreachable);
  118. cout<< "\nC++2y / C++26? lib:"<< endl;
  119. cout<< "\n"<< endl;
  120. }
  121. void show_sd6_feature (std::ostream& os) {
  122. using namespace std;
  123. os<< "-- Begin SD6 feature macros"<< endl;
  124. os<< "C++ general:"<< endl;
  125. os<< CXXOSHOWSUPPORT(__cpp_exceptions);
  126. os<< CXXOSHOWSUPPORT(__cpp_rtti);
  127. os<< CXXOSHOWSUPPORT(_EXCEPTIONS);
  128. os<< "C++11:"<< endl;
  129. os<< CXXOSHOWSUPPORT(__cpp_attributes);
  130. os<< CXXOSHOWSUPPORT(__cpp_constexpr);
  131. os<< CXXOSHOWSUPPORT(__cpp_decltype);
  132. os<< CXXOSHOWSUPPORT(__cpp_delegating_constructors);
  133. os<< CXXOSHOWSUPPORT(__cpp_inheriting_constructors);
  134. os<< CXXOSHOWSUPPORT(__cpp_lambdas);
  135. os<< CXXOSHOWSUPPORT(__cpp_range_based_for);
  136. os<< CXXOSHOWSUPPORT(__cpp_ref_qualifiers);
  137. os<< CXXOSHOWSUPPORT(__cpp_rvalue_references);
  138. os<< CXXOSHOWSUPPORT(__cpp_static_assert);
  139. os<< CXXOSHOWSUPPORT(__cpp_unicode_characters); // char16_t etc
  140. os<< CXXOSHOWSUPPORT(__cpp_unicode_literals);
  141. os<< CXXOSHOWSUPPORT(__cpp_user_defined_literals);
  142. os<< CXXOSHOWSUPPORT(__cpp_variadic_templates);
  143. os<< "C++14:"<< endl;
  144. os<< CXXOSHOWSUPPORT(__cpp_decltype_auto);
  145. os<< CXXOSHOWSUPPORT(__cpp_generic_lambdas);
  146. os<< CXXOSHOWSUPPORT(__cpp_return_type_deduction);
  147. os<< CXXOSHOWSUPPORT(__cpp_variable_templates);
  148. os<< "C++17:"<< endl;
  149. os<< CXXOSHOWSUPPORT(__cpp_inline_variables);
  150. os<< CXXOSHOWSUPPORT(__cpp_noexcept_function_type);
  151. os<< CXXOSHOWSUPPORT(__cpp_structured_bindings);
  152. os<< "C++20:"<< endl;
  153. os<< CXXOSHOWSUPPORT(__cpp_conditional_explicit);
  154. os<< "C++23:"<< endl;
  155. os<< CXXOSHOWSUPPORT(__cpp_size_t_suffix);
  156. os<< "C++26?:"<< endl;
  157. os<< "-- Done"<< endl;
  158. }
  159. void show_sd6_library (std::ostream& os) {
  160. using std::endl;
  161. os<< "\nC++14 lib:"<< endl;
  162. os<< CXXOSHOWSUPPORT(__cpp_lib_complex_udls);
  163. #if (defined(__cpp_lib_complex_udls))
  164. {
  165. using namespace std::literals::complex_literals;
  166. std::complex<float> test = 1.2f-3.4if;
  167. os<< test<< endl;
  168. }
  169. #endif
  170. os<< CXXOSHOWSUPPORT(__cpp_lib_exchange_function);
  171. os<< CXXOSHOWSUPPORT(__cpp_lib_integer_sequence);
  172. os<< CXXOSHOWSUPPORT(__cpp_lib_integral_constant_callable);
  173. os<< CXXOSHOWSUPPORT(__cpp_lib_make_reverse_iterator);
  174. os<< CXXOSHOWSUPPORT(__cpp_lib_make_unique );
  175. #if (defined(__cpp_lib_make_unique))
  176. {
  177. int p= *std::make_unique<int>(4);
  178. os<< "test: "<< p<< endl;
  179. }
  180. #endif
  181. os<< CXXOSHOWSUPPORT(__cpp_lib_quoted_string_io);
  182. #if (defined(__cpp_lib_quoted_string_io))
  183. {
  184. os<< "Test quoted: "<< std::quoted("Hello \"World\"!", '\'', '@')<< endl;
  185. }
  186. #endif
  187. os<< CXXOSHOWSUPPORT(__cpp_lib_robust_nonmodifying_seq_ops);
  188. os<< CXXOSHOWSUPPORT(__cpp_lib_string_udls);
  189. #if (defined(__cpp_lib_string_udls))
  190. {
  191. using namespace std::literals::string_literals;
  192. os<< "test UDL \"\"s: "<< "Test"s<< endl;
  193. }
  194. #endif
  195. os<< CXXOSHOWSUPPORT(__cpp_lib_transparent_operators);
  196. #if (defined(__cpp_lib_transparent_operators))
  197. {
  198. std::plus<void> P;
  199. os<< "test: "<< P(40,2)<< endl;
  200. }
  201. #endif
  202. os<< CXXOSHOWSUPPORT(__cpp_lib_tuples_by_type);
  203. os<< "\nC++17 lib:"<< endl;
  204. os<< CXXOSHOWSUPPORT(__cpp_lib_any);
  205. os<< CXXOSHOWSUPPORT(__cpp_lib_apply);
  206. os<< CXXOSHOWSUPPORT(__cpp_lib_as_const);
  207. os<< CXXOSHOWSUPPORT(__cpp_lib_bool_constant);
  208. #if (defined(__cpp_lib_bool_constant))
  209. {
  210. using cxxomfort::fix::to_value;
  211. os<< "test bool_constant<bool>: "<< std::flush;
  212. os<< to_value(std::bool_constant<true>())
  213. << " "<< to_value(std::bool_constant<false>())<< endl;
  214. }
  215. #endif
  216. os<< CXXOSHOWSUPPORT(__cpp_lib_byte);
  217. os<< CXXOSHOWSUPPORT(__cpp_lib_clamp);
  218. #if (defined(__cpp_lib_clamp))
  219. {
  220. int const z = -1;
  221. int const c= std::clamp(z, 0, 9); // clamp returns a potentially dangling ref
  222. os<< "test -1 into [0, 9]: "<< c<< endl;
  223. }
  224. #endif
  225. os<< CXXOSHOWSUPPORT(__cpp_lib_gcd_lcm);
  226. os<< CXXOSHOWSUPPORT(__cpp_lib_hypot);
  227. #if (defined(__cpp_lib_hypot))
  228. {
  229. os<< "test triangle hypot: "<< std::hypot(2.0, 3.0)<< endl;
  230. }
  231. #endif
  232. os<< CXXOSHOWSUPPORT(__cpp_lib_invoke);
  233. #if (defined(__cpp_lib_invoke))
  234. {
  235. os<< "test invoke printf: ";
  236. std::invoke(printf, "1 = %d", 1);
  237. os<< endl;
  238. }
  239. #endif
  240. os<< CXXOSHOWSUPPORT(__cpp_lib_is_aggregate);
  241. os<< CXXOSHOWSUPPORT(__cpp_lib_is_invocable);
  242. os<< CXXOSHOWSUPPORT(__cpp_lib_make_from_tuple);
  243. #if (defined(__cpp_lib_make_from_tuple) && CXXOMFORT_CXX_STD>=2011)
  244. {
  245. os<< "Test make string from tuple: "<< std::flush;
  246. auto t= std::make_tuple(4, 'h');
  247. auto mkt = std::make_from_tuple<std::string>(t);
  248. os<< cxxomfort::type_name<decltype(mkt)>()<< " : "<< mkt<< endl;
  249. }
  250. #endif
  251. os<< CXXOSHOWSUPPORT(__cpp_lib_nonmember_container_access);
  252. #if (defined(__cpp_lib_nonmember_container_access))
  253. {
  254. os<< "test nonmember container access on array 'arr': ";
  255. os<< "size "<< std::size(arr)<< " empty "<< std::empty(arr)<< " data "<< (void*)std::data(arr)<< endl;
  256. }
  257. #endif
  258. os<< CXXOSHOWSUPPORT(__cpp_lib_not_fn);
  259. os<< CXXOSHOWSUPPORT(__cpp_lib_raw_memory_algorithms );
  260. os<< CXXOSHOWSUPPORT(__cpp_lib_optional);
  261. os<< CXXOSHOWSUPPORT(__cpp_lib_string_view);
  262. #if (defined(__cpp_lib_string_view))
  263. {
  264. os<< "test: "<< std::string_view("Hello World!")<< endl;
  265. }
  266. #endif
  267. os<< CXXOSHOWSUPPORT(__cpp_lib_to_chars);
  268. os<< CXXOSHOWSUPPORT(__cpp_lib_variant);
  269. os<< CXXOSHOWSUPPORT(__cpp_lib_void_t);
  270. }