helper_macro_test.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // (C) Copyright John Maddock 2014-9.
  2. // (C) Copyright Andrey Semashev 2017.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/config.hpp>
  7. int test_fallthrough(int n)
  8. {
  9. switch (n)
  10. {
  11. case 0:
  12. n++;
  13. BOOST_FALLTHROUGH;
  14. case 1:
  15. n++;
  16. break;
  17. }
  18. return n;
  19. }
  20. int test_unreachable(int i)
  21. {
  22. if(BOOST_LIKELY(i)) return i;
  23. throw i;
  24. BOOST_UNREACHABLE_RETURN(0) // NOTE: no semicolon afterwards!!
  25. }
  26. BOOST_FORCEINLINE int always_inline(int i){ return ++i; }
  27. BOOST_NOINLINE int never_inline(int i){ return ++i; }
  28. BOOST_NORETURN void always_throw()
  29. {
  30. throw 0;
  31. }
  32. struct BOOST_MAY_ALIAS aliasing_struct {};
  33. typedef unsigned int BOOST_MAY_ALIAS aliasing_uint;
  34. struct BOOST_ATTRIBUTE_NODISCARD nodiscard_struct {};
  35. #define test_fallthrough(x) foobar(x)
  36. struct empty {};
  37. struct no_unique
  38. {
  39. int a;
  40. BOOST_ATTRIBUTE_NO_UNIQUE_ADDRESS empty b;
  41. };
  42. int main()
  43. {
  44. typedef int unused_type BOOST_ATTRIBUTE_UNUSED;
  45. try
  46. {
  47. int result = test_fallthrough BOOST_PREVENT_MACRO_SUBSTITUTION(0);
  48. BOOST_STATIC_CONSTANT(bool, value = 0);
  49. result += test_unreachable(1);
  50. result += always_inline(2);
  51. result += never_inline(3);
  52. if(BOOST_UNLIKELY(!result))
  53. always_throw();
  54. nodiscard_struct s;
  55. no_unique no_un;
  56. }
  57. catch(int)
  58. {
  59. return 1;
  60. }
  61. return 0;
  62. }