Guest User

Untitled

a guest
Dec 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. //
  2. // NOTES:
  3. // The assert macro is far from being production ready, the important part of the file
  4. // is the overload system :)
  5. // The idea was to find a way to have 2 version of assert like it exists for static_assert.
  6. // All the code compiled, with -std=c++11 -Wall -Werror -pedantic (or equivalent):
  7. // - Visual Studio 2017
  8. // - Clang 5.0
  9. // - GCC 7.0
  10. //
  11. // Known limitations:
  12. // - The overload system can't count from 0. 1 is the minimum with GCC and clang without gnu extensions.
  13. // - At the moment, the macro supports [1, 12] parameters. It is easy to add more (just tedious)
  14. //
  15.  
  16. #define PP_CONCAT(a, b) PP_CONCAT__0(a, b)
  17. #define PP_CONCAT__0(a, b) PP_CONCAT__1(a, b)
  18. #define PP_CONCAT__1(a, b) a ## b
  19.  
  20. #define PP_TO_STRING(a) PP_TO_STRING__0(a)
  21. #define PP_TO_STRING__0(a) PP_TO_STRING__1(a)
  22. #define PP_TO_STRING__1(a) #a
  23.  
  24. #if defined(_MSC_VER)
  25. #define PP_OVERLOAD(prefix, count_type, args) PP_CONCAT(prefix, count_type(PP_EXPAND_ARGS(args)))PP_EXPAND_ARGS(args)
  26. #define PP_COUNT_ARGS(...) PP_CONCAT(PP_COUNT_ARGS__0, PP_EXPAND_ARGS((__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)))
  27. #define PP_1_OR_MORE_ARGS(...) PP_CONCAT(PP_COUNT_ARGS__0, PP_EXPAND_ARGS((__VA_ARGS__, M, M, M, M, M, M, M, M, M, M, M, 1, 0)))
  28. #define PP_EXPAND_ARGS(args) args
  29. #else
  30. #define PP_OVERLOAD(prefix, count_type, ...) PP_CONCAT(prefix, count_type(__VA_ARGS__))(__VA_ARGS__)
  31. #define PP_COUNT_ARGS(...) PP_COUNT_ARGS__0(__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  32. #define PP_1_OR_MORE_ARGS(...) PP_COUNT_ARGS__0(__VA_ARGS__, M, M, M, M, M, M, M, M, M, M, M, 1, 0)
  33. #endif
  34. #define PP_COUNT_ARGS__0(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N, ...) N
  35.  
  36.  
  37.  
  38. #if defined(__linux__) || defined(__gnu_linux__)
  39. #define __debugbreak() __asm__("int3")
  40. #endif
  41.  
  42. #define ASSERT_IMP__1(cond) do { if (!(cond)) { __debugbreak(); } } while ((void)0, 0)
  43. #define ASSERT_IMP__M(cond, ...) do { if (!(cond)) { printf(__VA_ARGS__); __debugbreak(); } } while ((void)0, 0)
  44. #define assert(...) PP_OVERLOAD(ASSERT_IMP__, PP_1_OR_MORE_ARGS, __VA_ARGS__)
  45.  
  46. #include <stdarg.h>
  47. #include <stdio.h>
  48.  
  49. //#define static_assert(...)
  50.  
  51.  
  52. int main() {
  53. assert(true);
  54. assert(false, "Test %d\n", 1);
  55. }
Add Comment
Please, Sign In to add comment