expected.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //#include <nonstd/expected.hpp>
  2. #include <cxxomfort/base.hpp>
  3. #include <cxxomfort/string.hpp>
  4. #include <cxxomfort/expected.hpp>
  5. #include <cxxomfort/ostream.hpp>
  6. #include <cxxomfort/library/type_name.hpp>
  7. #include <cstdlib>
  8. #include <iostream>
  9. #include <string>
  10. //using namespace nonstd;
  11. //using namespace std::literals;
  12. typedef std::expected<int, std::string> Ecode_t;
  13. void test_expected();
  14. Ecode_t to_int( char const * const text )
  15. {
  16. char * pos = nullptr;
  17. long value = strtol( text, &pos, 0 );
  18. if ( pos != text ) return Ecode_t(value);
  19. else return std::make_unexpected( std::string("'") + text + "' isn't a number" );
  20. }
  21. int main( int argc, char * argv[] )
  22. {
  23. using namespace std;
  24. cxxomfort::output_info(stdout);
  25. cout<< endl;
  26. test_expected();
  27. char const* text = argc > 1 ? argv[1] : "a42";
  28. Ecode_t ei = to_int( text );
  29. if ( ei.has_value() ) std::cout << "'" << text << "' is " << *ei << ", ";
  30. else std::cout << "Error: " << ei.error();
  31. }
  32. void test_expected () {
  33. using namespace std;
  34. typedef expected<std::string_view, uint32_t> E1;
  35. cout<< cxxomfort::library::type_name<string_view>()<< " "<< sizeof(string_view)<< endl;
  36. cout<< cxxomfort::library::type_name<E1>()<< " "<< sizeof(E1)<< endl;
  37. typedef expected<double, uint32_t> E2;
  38. cout<< cxxomfort::library::type_name<double>()<< " "<< sizeof(double)<< endl;
  39. cout<< cxxomfort::library::type_name<E2>()<< " "<< sizeof(E2)<< endl;
  40. cout<< "A "<< alignof(uint32_t)<< endl;
  41. cout<< "A "<< alignof(double)<< endl;
  42. }