03-no-exceptions.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2016-2020 Martin Moene
  2. //
  3. // https://github.com/martinmoene/expected-lite
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #include "nonstd/expected.hpp"
  8. #include <iostream>
  9. template< typename T >
  10. void use( T const & /*x*/) {}
  11. #define expected_PRESENT( x ) \
  12. std::cout << #x << ": " << x << "\n"
  13. #define expected_ABSENT( x ) \
  14. std::cout << #x << ": (undefined)\n"
  15. void report()
  16. {
  17. #ifdef __cpp_exceptions
  18. expected_PRESENT( __cpp_exceptions );
  19. #else
  20. expected_ABSENT( __cpp_exceptions );
  21. #endif
  22. #ifdef __EXCEPTIONS
  23. expected_PRESENT( __EXCEPTIONS );
  24. #else
  25. expected_ABSENT( __EXCEPTIONS );
  26. #endif
  27. #ifdef _HAS_EXCEPTIONS
  28. expected_PRESENT( _HAS_EXCEPTIONS );
  29. #else
  30. expected_ABSENT( _HAS_EXCEPTIONS );
  31. #endif
  32. #ifdef _CPPUNWIND
  33. expected_PRESENT( _CPPUNWIND );
  34. #else
  35. expected_ABSENT( _CPPUNWIND );
  36. #endif
  37. }
  38. int violate_access()
  39. {
  40. nonstd::expected<int, char> eu( nonstd:: make_unexpected('a') );
  41. return eu.value();
  42. }
  43. int main()
  44. {
  45. report();
  46. #if ! nsel_CONFIG_NO_EXCEPTIONS_SEH
  47. return violate_access();
  48. #else
  49. __try
  50. {
  51. return violate_access();
  52. }
  53. __except( EXCEPTION_EXECUTE_HANDLER )
  54. {
  55. std::cerr << "\n*** Executing SEH __except block ***\n";
  56. }
  57. #endif
  58. }
  59. // -Dnsel_CONFIG_NO_EXCEPTIONS=1 automatically determined in expected.hpp
  60. // -Dnsel_CONFIG_NO_EXCEPTIONS_SEH=0 default:1 for msvc
  61. // cl -nologo -kernel -EHs-c- -GR- -I../include 03-no-exceptions.cpp && 03-no-exceptions
  62. // cl -nologo -kernel -EHs-c- -GR- -Dnsel_CONFIG_NO_EXCEPTIONS_SEH=0 -I../include 03-no-exceptions.cpp && 03-no-exceptions
  63. // g++ -Wall -fno-exceptions -I../include -o 03-no-exceptions 03-no-exceptions.cpp && 03-no-exceptions