Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // NOTES:
- // The assert macro is far from being production ready, the important part of the file
- // is the overload system :)
- // The idea was to find a way to have 2 version of assert like it exists for static_assert.
- // All the code compiled, with -std=c++11 -Wall -Werror -pedantic (or equivalent):
- // - Visual Studio 2017
- // - Clang 5.0
- // - GCC 7.0
- //
- // Known limitations:
- // - The overload system can't count from 0. 1 is the minimum with GCC and clang without gnu extensions.
- // - At the moment, the macro supports [1, 12] parameters. It is easy to add more (just tedious)
- //
- #define PP_CONCAT(a, b) PP_CONCAT__0(a, b)
- #define PP_CONCAT__0(a, b) PP_CONCAT__1(a, b)
- #define PP_CONCAT__1(a, b) a ## b
- #define PP_TO_STRING(a) PP_TO_STRING__0(a)
- #define PP_TO_STRING__0(a) PP_TO_STRING__1(a)
- #define PP_TO_STRING__1(a) #a
- #if defined(_MSC_VER)
- #define PP_OVERLOAD(prefix, count_type, args) PP_CONCAT(prefix, count_type(PP_EXPAND_ARGS(args)))PP_EXPAND_ARGS(args)
- #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)))
- #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)))
- #define PP_EXPAND_ARGS(args) args
- #else
- #define PP_OVERLOAD(prefix, count_type, ...) PP_CONCAT(prefix, count_type(__VA_ARGS__))(__VA_ARGS__)
- #define PP_COUNT_ARGS(...) PP_COUNT_ARGS__0(__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
- #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)
- #endif
- #define PP_COUNT_ARGS__0(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N, ...) N
- #if defined(__linux__) || defined(__gnu_linux__)
- #define __debugbreak() __asm__("int3")
- #endif
- #define ASSERT_IMP__1(cond) do { if (!(cond)) { __debugbreak(); } } while ((void)0, 0)
- #define ASSERT_IMP__M(cond, ...) do { if (!(cond)) { printf(__VA_ARGS__); __debugbreak(); } } while ((void)0, 0)
- #define assert(...) PP_OVERLOAD(ASSERT_IMP__, PP_1_OR_MORE_ARGS, __VA_ARGS__)
- #include <stdarg.h>
- #include <stdio.h>
- //#define static_assert(...)
- int main() {
- assert(true);
- assert(false, "Test %d\n", 1);
- }
Add Comment
Please, Sign In to add comment