Questions tagged [variadic-macros]

Variadic macros are a feature of the C-preprocessor that permit a variable number of arguments to a macro. They were added in the 1999 revision of the C standard.

Variadic macros are a feature of the C-preprocessor that permit a variable number of arguments to a macro. They were added in the 1999 revision of the C standard.

More details can be obtained from:

295 questions
0
votes
1 answer

How to access variable values of a variadic print function?

I am trying to test a function (bar()) which uses a module specific print function (foo_print) to output to the console, seen below: #define foo_print(...) foo_log(FOO_LOGTYPE_PRINT, ## __VA_ARGS__) where foo_log() is defined as an item of…
0
votes
1 answer

Is it possible to make a macro to reflect a C++ function declaration? And how?

I want to save and access at runtime the types and default values of inputs and outputs of a function. I have some structs to hold what I need for now in FunDeclaration and FunParam and an example function foo to reflect. Live code here:…
FrameBuffer
  • 757
  • 1
  • 7
  • 26
0
votes
1 answer

Why does this variadic C function not print the first argument?

I am trying to make a variadic function in C with stdarg.h, and I followed this tutorial and wrote the code exactly as he did: https://www.youtube.com/watch?v=S-ak715zIIE. I included the output in the code. I cannot figure out why the first argument…
Caspian Ahlberg
  • 934
  • 10
  • 19
0
votes
0 answers

Variadiac Macros to create class definition with unknown number of member variables of different types, with initializer list

I'm trying to reduce boilerplate class definitions, while also making it easy for me to change them in the future without having to go back and updating all of them manually I'm wondering if I just need to make a script to generate these classes or…
0
votes
0 answers

MSVC Preprocessor Bug - Overloading C/C++ Macro on Argument Count

I'm trying to write an overloaded macro to format StringMap entries, and cannot get the preprocessor to recognize when there is more than 1 argument. I have looked at virtually all similar questions and have tried 5+ implementations but none work.…
0
votes
1 answer

How to write a macro that will be expanded to define an object with template parameters

Is it possible to write a macro, which, in turn, creates an object definition from it, for example: DEFINE_OBJECT_INVALID_VALUES(ObjectType, double, int, 1.0, 1) should become: ObjectType a(1.0,…
Vardan Hovhannisyan
  • 1,101
  • 3
  • 17
  • 40
0
votes
2 answers

C/C++ Variadic Macro Function Overloading

I am trying to build a simple c++ logger for learning purposes, but I seem to be stuck at the following issue. Let's say that I have a namespace that contains two functions as follows: namespace l { void _p(const char* file, const int line, int…
asdf
  • 460
  • 1
  • 10
  • 31
0
votes
1 answer

Is it possible to get macro passed as optional argument to variadic function using va_arg

I have the below program. It passes a macro as optional arg to variadic function. Within that function definition, shall we able to get that macro without expansion. I have used type as 'char *' and is showing the macro expanded string. Any ways to…
0
votes
1 answer

Macro with number of arguments AND default parameter

I'm familiar with the following way of creating a macro with variable number of arguments. However, consider: #define MY_MACRO_N(value, format, ...) my_func(value, format, ##__VA_ARGS__) #define MY_MACRO_0(value) my_func(value, NULL) Where my_func…
galah92
  • 3,621
  • 2
  • 29
  • 55
0
votes
1 answer

How to do a variadic macro and or template and or function that captures the expression as well as its evaluation?

I have overloaded a class with various << operators inline QString operator<<(bool boolean) { return (boolean ? QString("true") : QString("false")); } inline QString operator<<(char const *string) { return…
Anon
  • 2,267
  • 3
  • 34
  • 51
0
votes
1 answer

C++ preprocessing generate variable member, setters and map

I'm attempting to generate some code with preprocessor. In some class, I need to define multiple variable member, their corresponding setters and a map containing reference on each declared variable. To illustrate my need, you can find the following…
xamix
  • 13
  • 5
0
votes
2 answers

How to easily create fully "variadic" functions with C++ 98 standard?

The library https://github.com/c42f/tinyformat/blob/2f9335afd9941688e42d60cae5166b9f0600b2d1/tinyformat.h#L1104-L1116, uses this awesome trick to do "variadic" templates on C++ 98: inline void printfln(const char* fmt) { format(std::cout,…
0
votes
2 answers

How to add an extra argument to a C++ log macro

Suppose I have below macro for logging TRACE(type, sub-type, message, fmt, args); Now I have a requirement to punch extra argument, say machine IP to this log: #include char *IP = "100.200.200.100"; #define TRACE(type, sub-type, message,…
UserJJ
  • 3
  • 3
0
votes
2 answers

Expanding to different values based on length of __VA_ARGS__

Suppose we've got the following two functions: void foo1(int p); void foo2(int p, ...); I'd like to write a macro to automatically expand to the proper one based on the number of arguments. I've used the following dirty/hacky way, but I'm curious…
frogatto
  • 28,539
  • 11
  • 83
  • 129
0
votes
1 answer

How to define a variadic macro beside a non-variadic one?

The best way is to show it with code. I'm compiling this as a Win32 C++ project under Visual Studio 2017: #include #include //Trace outout #ifdef _DEBUG #define TRACE(s) ::OutputDebugString(s) #define TRACE(s, ...) \ {…
c00000fd
  • 20,994
  • 29
  • 177
  • 400