Answers for "c++ macro variable args"

1

variadic macros

// variadic_macros.cpp
#include <stdio.h>
#define EMPTY

#define CHECK1(x, ...) if (!(x)) { printf(__VA_ARGS__); }
#define CHECK2(x, ...) if ((x)) { printf(__VA_ARGS__); }
#define CHECK3(...) { printf(__VA_ARGS__); }
#define MACRO(s, ...) printf(s, __VA_ARGS__)

int main() {
    CHECK1(0, "here %s %s %s", "are", "some", "varargs1(1)n");
    CHECK1(1, "here %s %s %s", "are", "some", "varargs1(2)n");   // won't print

    CHECK2(0, "here %s %s %s", "are", "some", "varargs2(3)n");   // won't print
    CHECK2(1, "here %s %s %s", "are", "some", "varargs2(4)n");

    // always invokes printf in the macro
    CHECK3("here %s %s %s", "are", "some", "varargs3(5)n");

    MACRO("hello, worldn");

    MACRO("errorn", EMPTY); // would cause error C2059, except VC++
                             // suppresses the trailing comma
}
Posted by: Guest on July-31-2020
0

how to call a function in a macro with variadic arguments c++

#define eprintf(…) fprintf (stderr, __VA_ARGS__)
Posted by: Guest on July-31-2020

Browse Popular Code Answers by Language