0

I try overload WriteLogs macro with 1 and 2 params:

template <typename... Args>
void WriteLog(const LogContext& logContext, const LogSettings& logSettings, std::string formatMsg, Args... args) {
    // ...
    printf("formatMsg", args...);
    // ...
}

#define WriteLog_1(message, ...) WriteLog(LogContext{__LINE__, __FILE__, __FUNCTION__}, LogSettings{false, 0}, message, ##__VA_ARGS__)
#define WriteLog_2(settings, message, ...) WriteLog(LogContext{__LINE__, __FILE__, __FUNCTION__}, settings, message, ##__VA_ARGS__)

#define EXPAND(x) x
#define GET_MACRO(_1, _2, NAME, ...) NAME
#define WriteLogs(...) EXPAND(GET_MACRO(__VA_ARGS__, WriteDebug2, WriteDebug1)(__VA_ARGS__))

so is it possible to overload them for usage:

int main() {
   WriteLogs("hello %s, %d + %d = %d", "world", 2, 2, 5); // WriteLog_1
   WriteLogs(LogSettings{true, 0}, "hello %s, %d + %d = %d", "world", 2, 2, 5); // WriteLog_2
}
isrepeat
  • 113
  • 5
  • Rather than attempting an overload with the preprocessor, just write two `WriteLog` functions. – HolyBlackCat Dec 13 '22 at 19:08
  • I need use preprocessor to avoid writing explicilty "LogContext{__LINE__, __FILE__, __FUNCTION__}" – isrepeat Dec 13 '22 at 19:30
  • 3
    What I'm saying is, keep the macro but without any complex logic, somethinig like `#define WriteLogs(...) WriteLog(LogContext{...}, __VA_ARGS__)`. Then make two `WriteLog` functions to make it work with and without the extra argument. – HolyBlackCat Dec 13 '22 at 19:33

0 Answers0