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
2
votes
2 answers

Comma in C/C++ macro passed to another macro

I have these macros which generate error in Visual Studio 2015. #define log_params __FILE__, __LINE__ #define log(file, line, message, ...) _snprintf_s(nullptr, 0, 0, message, __VA_ARGS__) Now calling this never works log(log_params, "testing…
Roozbeh G
  • 427
  • 4
  • 18
2
votes
1 answer

What are options to get multiple variable lists in a variadic macro?

I have been trying to use variadic macros to reduce redundant code in some SFINAE patterns I am using. Specifically, I would like to generate function definitions using variadic macros. (This question is not about SFINAE). For my application, I…
doc07b5
  • 600
  • 1
  • 7
  • 18
2
votes
2 answers

Count number of elements for static array initialization in C

There is the following code (C99): #define MAX_ALLOCATIONS 2 #if !defined(ALLOCATIONS) #define ALLOCATIONS {{1, 0, 0, 64},{1, 0, 0, 32}} #endif struct allocation { int thread_count_; int node_run_; int node_alloc_; int…
Dimon
  • 436
  • 5
  • 15
2
votes
1 answer

Cross-platform macro wrapper for fprintf()

Is there a cross-platform approach to wrapping fprintf() so I that I can have a simple logging function for dumping logs to either files or the console, and utilizes a simple C printf() style format string and arguments list? Right now, I'm doing…
Cloud
  • 18,753
  • 15
  • 79
  • 153
2
votes
1 answer

Limit number of expanded arguments with __VA_ARGS__

I am overloading macros based on the number of arguments, as described in this question : Overloading Macro on Number of Arguments When expanded, the __VA_ARGS__ "push" the macro names so that the right macro name is selected, based on the number of…
Virus721
  • 8,061
  • 12
  • 67
  • 123
2
votes
2 answers

Is va_start required in variadic arguments for functions?

I'm reading the text The Linux Programming Interface and they show this function to handle errors. In the man pages (man stdarg) it says va_start must be called first to initialize ap for use by va_arg() and va_end. So why in this function there…
user1527227
  • 2,068
  • 5
  • 25
  • 36
2
votes
3 answers

Different behavior in visual c++ versus gcc/clang while stringifying parameter which contains comma

I'm using stringizing operator to convert parameter which may contains comma passed to a macro into string. As I know, some characters cannot be stringified – notably, the comma(,) because it is used to delimit parameters and the right…
jfly
  • 7,715
  • 3
  • 35
  • 65
2
votes
3 answers

Objective C - #define using __VA_ARGS__

I am learning how to use macro but now confused with one. I am trying to create a NSString concatenate which will just append every params to each other. for example : concatOP(@"hey",@"Jude",@"Don't") would return a NSString containing :…
2
votes
2 answers

"Uninitialised value was created by a stack allocation" when using variadic macro and function

I have the following program http://ideone.com/1RPs8E . It uses a variadic function tlog that will print a line in a log file. It receives a level for the line printed, a file name, a line and a function for debug information and a format for printf…
Victor Dodon
  • 1,796
  • 3
  • 18
  • 27
2
votes
3 answers

Variadic macro expanding

I want to know that is there any way to call a C VARIADIC MACRO selectively. First, let me show some code I want to achieve: #include #define _VA_NARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N #define _VA_NARGS(...)…
glowseed
  • 67
  • 2
  • 9
2
votes
1 answer

Translate sequence in macro parameters to separate macros

How to acces each element in macro if the definition is like MACRO(name, seq) and the code is like: MACRO("TheName", (Elem1) (Elem2) (Elem3) ) I want to generate the next code: MACRO("TheName", ELEMMACRO(Elem1) ELEMMACRO(Elem2) …
Alex Tiger
  • 377
  • 3
  • 22
2
votes
1 answer

Adding extra arguments to a C-style variadic argument list

I'm trying to write a wrapper for a C-style variadic function like printf, which adds some extra arguments, but I'm having trouble with it: void printf(char* fmt, ...); // the thing I'm trying to wrap void wrapper(char* fmt, ...) { printf(fmt,…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
2
votes
2 answers

#ifdef inside a macro call works with gcc but not with msvc

I am have a macro TYPELIST which takes variadic arguments. I want to have something like typedef TYPELIST(A ,B ,C ,D #ifdef BLA_ ,E #endif ,F) This works perfectly with…
Shubham
  • 352
  • 3
  • 14
2
votes
2 answers

Variadic macros arguments

I have an assert macros which looks like: #define ASSERT(condition, ...) \ (condition) ? (void)0 : MyLogFunction(__LINE__, __VA_ARGS__) MyLogFunction is a variadic template too: template void MyLogFunction(int line, const…
Max Frai
  • 61,946
  • 78
  • 197
  • 306
2
votes
1 answer

Variadic Macros need a unmeaningful macro to let it work?

The following code is okay on VC++ 2012 #include #define MAX_OF_2(a,b) std::max(a,b) #define FOO(a) a // work #define MAX2(...) FOO(MAX_OF_2(__VA_ARGS__)) // Not work // #define MAX2(...) …
user1899020
  • 13,167
  • 21
  • 79
  • 154