0

I have the following macro:

#define MyAssert(condition, ...) CallMyAssertFunctionInCpp(condition, __VA_ARGS__)

__VA_ARGS__ is the error message of type char *, I would like to make it equal to "" if __VA_ARGS__ is empty.

How to do that? I'm not sure I can do condition in macro but maybe there is a trick?

Fractale
  • 1,503
  • 3
  • 19
  • 34

1 Answers1

3

As suggested in comments, it's easy to achieve using C++ templates:

#include <utility>

const char * CallMyAssertFunctionInCpp(bool condition, int arg);

template <typename... T>
const char * MyAssert(bool condition, T... args) {
    if constexpr (sizeof...(T) == 0) {
        return "";
    } else {
        return CallMyAssertFunctionInCpp(condition, std::forward<T>(args)...);
    }
}

void example() {
    MyAssert(true);
    MyAssert(true, 42);
}
Mathias Rav
  • 2,808
  • 14
  • 24