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

Expand Variadic Template in Vardiadic Macro (how to extract arguments names from a target function)

I am struggling with challenging, but yet simple problem. Let say that I have a target function as follow void target_fnc(int arg1, double arg2) { /* do something here */ } and what I would like to "extract" is the variable names (i.e. 'arg1',…
2
votes
2 answers

How to enhance this variable-dumping debug macro to be variadic?

First working code: #include // NOTE: requires compiler which has __PRETTY_FUNCTION__, like gcc or clang #define DUMP(v) std::cerr << __PRETTY_FUNCTION__ << ':' << __LINE__ << ':' << #v << "='" << (v) << "'\n" int main(int argc) { …
hyde
  • 60,639
  • 21
  • 115
  • 176
2
votes
2 answers

reverse the arguments to a variadic macro

How do I reverse the arguments to a variadic macro? For example, I'd like #define REVERSE(...) ??? REVERSE(A,B,C) // expands to C,B,A My goal is to separate the front and back arguments: #define APPLY(FUN,...) FUN(__VA_ARGS__) #define…
joshuanapoli
  • 2,509
  • 3
  • 25
  • 34
2
votes
2 answers

C/C++ preprocessor: how to generate code depending on number of arguments

I want to be able to do this in my preprocessor macros #define dble double /* Just something that converts x to a double precision float. static_cast used as an illustration: */ #define dble(x) static_cast(x) I know that overloading is…
2
votes
2 answers

How can i generate variadic macro for concatenate string

I got stuck here... #define CONCAT(a,b) BOOST_PP_STRINGIZE(BOOST_PP_CAT(a,b))#define CONCAT1(a,b,c) CONCAT(CONCAT(a,b),c) and so on. How i can to generate the CONCAT macro even if 20 arguments? May be i can to use BOOST_PP_SEQ_FOR_EACH but i don't…
1
vote
1 answer

2 different syntax for variadic macros

#define TEST(X, ...) X ## __VA_ARGS__ // (1) #define TEST(X, args...) X ## args // (2) Is there any functional difference between them ? (i.e. one of them can be used in a better way then other in certain cases). Also, are both…
iammilind
  • 68,093
  • 33
  • 169
  • 336
1
vote
1 answer

Concatenate __VA_ARGS__ into a single string using C macros

I'm trying to create a macro that expands into font effect escape codes for the terminal. Here's two and three argument examples: #define FONT_FX2(arg1, arg2) "\x1b[" #arg1 ";" #arg2 "m" #define FONT_FX3(arg1, arg2, arg3) "\x1b[" #arg1 ";" #arg2 ";"…
ealker
  • 68
  • 7
1
vote
0 answers

CUDA C++ BlockReduce sum multiple variables using macro

#include #include #include using namespace std; __device__ void BlockReduce(double val1, double val2, double* shmem1, double* shmem2) { int num_warp = blockDim.x / 32; int warp_id = threadIdx.x / 32, lane_id…
Huy Le
  • 1,439
  • 4
  • 19
1
vote
1 answer

Pass undefined number of template classes (having commas) as macro argument

TL;DR What are the available tricks to pass undefined number of template classes to a macro in C++17 ? Example: my_macro(std::map>, double, std::deque>) With context I'm in a context where I…
Caduchon
  • 4,574
  • 4
  • 26
  • 67
1
vote
2 answers

Suppressing warnings for a printf-like function in C++

I have a legacy logging printf-like function in C: namespace legacy { void DoLog(const char* format,...); } If is only visible if a corresponding macro is defined (this is legacy too): #ifdef LOG # define Log legacy::DoLog #else # define Log /*…
1
vote
1 answer

Adding Variadic macros to a header file (program interface)

I'm wondering if I'm using variadic macros to create a function with default arguments, how should I add the prototype of this function to the interface header file So that I hide the base function from the user. #define…
1
vote
1 answer

Wrapping variadic arguments in a c macro

So, I want to make a function(-like macro) that takes any number of arguments of different types and does something to it. I mean, I did manage to make it work, but I'm looking for a more elegant solution (or to make sure my way is the way it should…
DiKetarogg
  • 170
  • 8
1
vote
1 answer

C/C++ expand multiple macros into one variadic macro

Background I am working on an existing codebase which uses a macro pattern to generate boilerplate methods similar to this: START_MAP() MAP_ENTRY(a) MAP_ENTRY(b) // ..... MAP_ENTRY(z) END_MAP() I am re-implementing the code that these…
Jerfov2
  • 5,264
  • 5
  • 30
  • 52
1
vote
3 answers

Can `#ifdef` be used inside a macro?

I only found this related question, which isn't quite what I am looking for. I used to have macros defined inside an #ifdef statement: #ifdef DEBUG # define PRINT_IF_DEBUGGING(format) printf(format); # define PRINTF_IF_DEBUGGING(format, ...)…
Eduardo Reis
  • 1,691
  • 1
  • 22
  • 45
1
vote
2 answers

Converting a variadic macro to a variadic template function?

Given a variadic macro of the form: #define MY_CALL_RETURN_F(FType, FId, ...) \ if(/*prelude omitted*/) { \ FType f = (FType)GetFuncFomId(FId); \ if(f) { \ return f(__VA_ARGS__); \ …