string.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <cxxomfort/cxxomfort.hpp>
  2. #include <cxxomfort/library/type_name.hpp>
  3. #include <cxxomfort/string.hpp>
  4. #include "../library/affixes/string.hpp"
  5. #include <iostream>
  6. // reference:
  7. // https://en.cppreference.com/w/cpp/language/user_literal
  8. template <typename T>
  9. void show (std::ostream& os, T const& t) {
  10. using namespace std;
  11. using cxxomfort::library::type_name;
  12. os<< "[type:"<< type_name<T>();
  13. os<< " sizeof:"<< sizeof(T);
  14. os<< " value:'"<< t<< "' ] "<< endl;
  15. }
  16. template<> void show (std::ostream& os, std::wstring const& t) {
  17. using namespace std;
  18. using cxxomfort::library::type_name;
  19. string T= type_name<wstring const>();
  20. os<< "[type:"<< T.c_str();
  21. os<< " sizeof:"<< sizeof(T);
  22. os<< " value:'"<< string(t.begin(), t.end())<< "' ] "<< endl;
  23. }
  24. int main () {
  25. using namespace std;
  26. using namespace cxxomfort::library::string::literals;
  27. using cxxomfort::library::type_name;
  28. cout<< "In C++ the type of a string literal is const char[N].\n"<< endl;
  29. #if (CXXOMFORT_CXX_STD>=2011)
  30. auto foo = "Hello World";
  31. cout<< "Type of string literal: "<< type_name<decltype(foo)>()<< " ; "<< typeid(foo).name()<< endl;
  32. cout<< " passed directly: "<< type_name<decltype("Hello World")>()<< " ; "<< typeid("Hello World").name()<< endl;
  33. #endif
  34. cout<< "\n 1 - Using as suffixes: value | foo "<< endl;
  35. cout<< "With \"chars...\" : "; show(cout, "Hello World1"); // should be: const char[]
  36. cout<< "With std::string : "; show(cout, "Hello World2" | s); // should be: std::string
  37. cout<< "With std::wstring : "; show(cout, L"Hello World3" | s);
  38. cout<< "With AESTHETICS : "; show(cout, "~*Hello World*~ ^_^ " | aesth);
  39. cout<< endl;
  40. // cool thing: they also work as normal functions (function objects)
  41. cout<< "\n 2 - Using as function objects: foo ( value ) "<< endl;
  42. cout<< "Expecting string 5 : "<< flush; show(cout, s("Hello World5")); // should be: std::string
  43. cout<< "Expecting AESTHETIC : "<< flush; show(cout, aesth("Hello World6"));
  44. cout<< endl;
  45. // and in C++11 onwards, as literal suffixes
  46. #if (CXXOMFORT_CXX_STD >= 2011)
  47. cout<< "\n 3 - Using as C++11 suffixes: value_foo "<< endl;
  48. cout<< "Expecting string 4 : "; show(cout, "Hello World4"_s); // should be: std::string
  49. cout<< "Expecting wstring 5 : "; show(cout, L"Hello World5"_s);
  50. #endif
  51. }