integer.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <cxxomfort/cxxomfort.hpp>
  2. #include <cxxomfort/cstdint.hpp> // integer types
  3. #include <cxxomfort/library/type_name.hpp>
  4. #include <iostream>
  5. #include "../library/literals.hpp"
  6. #include "../library/affixes/integer.hpp"
  7. // reference:
  8. // https://en.cppreference.com/w/cpp/language/user_literal
  9. template <typename T>
  10. void show (std::ostream& os, T const& t) {
  11. using namespace std;
  12. using cxxomfort::library::type_name;
  13. os<< "[type:"<< type_name<T>()<< " sizeof:"<< sizeof(T)<< " value:"<< t<< " ] "<< endl;
  14. }
  15. int main () {
  16. using namespace std;
  17. using namespace cxxomfort::literals;
  18. using namespace cxxomfort::library::literals;
  19. using cxxomfort::library::type_name;
  20. cout<< " 1, int : "; show(cout, 4); // should be: int
  21. cout<< type_name<size_t>()<< endl;
  22. cout<< " 1, size_t : "; show(cout, 2524 | zu); // should be: size_t
  23. cout<< type_name<unsigned short>()<< endl;
  24. cout<< " 1, u.short : "; show(cout, 2524 | hu); // should be: unsigned short
  25. cout<< type_name<uintmax_t>()<< endl;
  26. cout<< " 1, uintmax : "; show(cout, 2524 | ju); // should be: uintmax
  27. cout<< endl;
  28. show(cout, 400 | dbl); // should be: 800
  29. // cool thing: they also work as normal functions (function objects)
  30. cout<< "\n 2 - Using as function objects: foo ( value ) "<< endl;
  31. cout<< "signed : "; show(cout, hi(9)); // should be: unsigned short
  32. cout<< "unsigned : "; show(cout, hu(-10)); // should be: unsigned short, but should throw a sign conversion warning
  33. cout<< endl;
  34. // and in C++11 onwards, as literal suffixes
  35. #if (CXXOMFORT_CXX_STD >= 2011)
  36. cout<< "\n 3 - Using as C++11 suffixes: value_foo "<< endl;
  37. show(cout, 17417_hu); // should be: unsigned short
  38. show(cout, 17418_zu); // should be: size_t
  39. constexpr auto xi = 5000_hu;
  40. cout<< "type of xi: "<< type_name<decltype(xi)>()<< endl;
  41. #endif
  42. }