306 questions
1
vote
0
answers
59
views
Invalid preprocessor directive include inside variadic macro
In C I wanted to use preprocessor directives inside a macro. Since this doesn't work, I stumbled over this answer about variadic macros, where a macro accepts a varying amount of arguments.
The answer ...
0
votes
0
answers
85
views
How to simplify the similar macros?
It's a pity that there are a lot similar macros(both name and function) in the code below. How to simplify the similar macros?
The difference between the similar macros are the string tags, say [...
0
votes
1
answer
56
views
Troubleshooting C Macro Expansion Compilation Errors about format specifiers
Recently, I was working to get and set some fields on a UCI file. To get the failure reason appropriately, I decided to use some macro as given below:
#define HANDLE_UCI_ERR(_cond_, _fmt_, ...) ...
1
vote
0
answers
98
views
C++ preprocessor macro with and without arguments
I would like to call two different macros based on whether the macro invocation was with or without parameters. Based on the many examples on SO regarding specific numbers of parameters, I came up ...
0
votes
2
answers
149
views
How can you define std::variant at compile time if the dependent types are also defined at compile time?
Is it possible to use a macro or something similar to create instances of a template class and generate the code that then adds the created variables to a std::vector that uses std::variant?
Consider ...
1
vote
2
answers
137
views
Can __VA_OPT__(,) detect a trailing comma with nothing after it?
While playing with __VA_OPT__(,) I noticed the following behavior.
Background
First, note that C is fine with trailing commas in an initializer. These are equivalent:
int foo[] = { 10, 20, 30 };
int ...
0
votes
1
answer
544
views
Stringify each token in __VA_ARGS__ from a variadic macro [duplicate]
I am trying to #stringify each token in a __VA_ARGS__ from a variadic macro.
The idea is to use these tokens as entries in an enum and also push them (stringified) to a std::vector<std::string>. ...
1
vote
1
answer
80
views
why can't this macro be expanded when using wrapping macro of token pasting operator multiple times
I want to implement some function related to reflection in c++, but some issue occurred when I want to expand macro which be concatenated by two macro has been expanded.
I try reproducing this issue ...
3
votes
1
answer
560
views
Getting __VA_OPT__ to be recognized by Visual Studio?
Tried to set /std:c++20 or /std:c++latest along /Zc:preprocessor as mentioned in the documentation but Visual Studio refuses to recognize __VA_OPT__ in the following snippet:
#define _sdk_log(fmt, ...)...
4
votes
1
answer
68
views
variadic macro doesn't compile
I have 2 variadic macros, one of them compiles fine and the other one doesn't:
#define ASSERT(x, ...) assert_log(x, __FILE__, __LINE__, __VA_ARGS__)
#define TRACE (x, ...) ...
1
vote
3
answers
160
views
How to perform different behaviors based on the length of variadic args in cpp macro?
I'm trying to make a short-hand for logging (log4cplus) + libfmt. I have defined a macro in my util header:
#define T_TRACE(fs, ...) \
LOG4CPLUS_TRACE(Logger::Instance().logger, fmt::format(fs, ...
0
votes
1
answer
155
views
Is there a way to call a macro on every argument in __VA_ARGS__?
for example:
#define M(x) do { x++; f(x); } while(0)
#define M_ALL(...) do { ??? } while(0)
Is there a way to call M on each of the arguments of M_ALL?
such that doing this:
M_ALL(x, y, z)
would ...
0
votes
0
answers
65
views
How to get this expected behavior using MSVC macros?
The behavior i want
#define E() E2(k)
#define E2(k) printf("%d", k);
#define A A2(__COUNTER__)
#define A2(k) E()
A
I would like to E() expands FIRTS to E2(k) in the line #define A2(k) E(). ...
1
vote
1
answer
184
views
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 ";" #...
0
votes
2
answers
347
views
How can a variadic macro with a list of types produce a function which accepts those arguments and passes them along?
I have a macro:
#define WRAP_FUNCTION(wrapper_name, function, ret_type, arg1_type, arg2_type, ...)
And I would like it to define a function like this:
ret_type wrapper_name(arg1_type arg1, arg2_type ...
0
votes
1
answer
96
views
How can I use DataTypes key as arguments
I have a macro that works as expected, but I want to make some changes to make it cleaner to use.
#define FuncCreate(func_name, func, ...) \
int func_name(lua_State *ms) { \
func(__VA_ARGS__); ...
2
votes
2
answers
78
views
Variadic macros to create multiple constructs with delimiters
EDIT2: Figured it out. See my answer below.
Some background:
I am using an SQL library that returns query results as tuples. For each DB statement I write:
An SQL query that includes a list of ...
1
vote
0
answers
90
views
CUDA C++ BlockReduce sum multiple variables using macro
#include <cuda_runtime.h>
#include <iostream>
#include <string>
using namespace std;
__device__
void BlockReduce(double val1, double val2, double* shmem1, double* shmem2)
{
int ...
0
votes
0
answers
267
views
How is stdarg Provided in the C++ Runtime
Background:
While setting up YouCompleteMe compile arguments for a standalone GCC + sysroot for aarch64, I went spelunking in the standard C++ system includes and found this little gem:
[sysroot]/usr/...
1
vote
1
answer
75
views
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<std::string,std::vector<double>>, double, std::deque<...
-1
votes
1
answer
300
views
Is there a way to select between two macros depending on the number of parameters on a function sent as the macro parameter?
I realize the title of the question is very confusing, but I cannot think of a better way to word this, so I'll explain it better with code.
I know you can select macros based on the number of ...
0
votes
1
answer
226
views
How to prefix __VA_ARGS__ elements in a macro
Is it possible to prefix each element of variadic parameters with something else?
#define INPUTS(...) ???
Function(INPUTS(a, b, c))
should become
Function(IN a, IN b, IN c)
1
vote
2
answers
256
views
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
...
1
vote
1
answer
50
views
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 ...
0
votes
1
answer
67
views
Can an argument follow variadics parameters in C?
Is inserting a terminating argument in send() following the VA_ARGS allowed in the specs?
#define VALIST_TERMINATOR NULL
#define send(targ, ...)(compile_args(targ, __VA_ARGS__, VALIST_TERMINATOR))
...
0
votes
0
answers
58
views
Is it possible to overload macros that contain VA_ARGS for logs?
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... ...
4
votes
1
answer
128
views
variadic format strings in C macros?
I'm trying to write a basic macro like this:
#define USER_ERROR(fmt, ...) { \
fprintf(stderr, "ERROR %s(): %s\n", __func__, fmt, ##__VA_ARGS__); \
} \
my ideal usage:
USER_ERROR("...
1
vote
1
answer
478
views
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 ...
-1
votes
1
answer
70
views
most succinct or canonical way to bind class members for purposes of input, output, etc. using power of modern C++
My application requires end-users to write some classes to which I'll feed input data and take output data from. These user classes can be subclasses of a base class I provide. A way that's worked ...
0
votes
0
answers
102
views
Propagate __VA_ARGS__ macro types inside template methods
I was wondering if there was a clean method to extract types from __VA_ARGS__ and use them to fill template method or structure definitions?
Thanks in advance
I have the following problem:
my macro ...
0
votes
0
answers
230
views
C, MSVC, __VA_ARGS__, Error calculating the number of variable parameters when 0 parameters
#include <stdarg.h>
#define VARGS_CNT(...) VARGS_CNT_LOCAL(, ##__VA_ARGS__,\
64, 63, 62, 61, 60, \
59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
39, 38,...
1
vote
1
answer
304
views
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()...
1
vote
1
answer
3k
views
TypeScript macro expansion
I need to reproduce the functionality of this C code but in typescript. The purpose of this code is mainly to simplify error checking as stated in JPL's The Power of Ten but I couldn't find a way to ...
2
votes
2
answers
257
views
Overload macro as variable and function
First, there are a lot of posts on overloading macros:
Can macros be overloaded by number of arguments?
C++ Overloading Macros
Macro overloading
Overloading Macro on Number of Arguments
etc. ...
...
0
votes
1
answer
2k
views
Access the contents of __VA_ARGS__ in a macro (not a function)
One can access the contents of ... inside a function using stdarg.h:
void fn(int nargs, ...){
va_list args; va_start(args,nargs);
i64 arg0 = va_arg(args,i64);
va_end(args);
}
The only way I ...
0
votes
1
answer
1k
views
Default arguments to C macros
Suppose I have function bshow() with signature
void bshow(int arg0, int arg1, int arg2);
but for arbitrary reasons I want to implement it as a macro.
Furthermore, I want the function have default ...
1
vote
3
answers
618
views
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);...
0
votes
1
answer
320
views
Variadic macros in C
In GNU documentation on variadic macros, an example is
#define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)
...
In standard C, you are not allowed to leave the variable argument out ...
0
votes
1
answer
570
views
Using __VA_ARGS__ in a nested macro, but the args are truncated
I'm doing something like below:
#define AA(mac, a, ...) mac(a, __VA_ARGS__)
#define MAC1(a, b, c) a##b##c
AA(MAC1, 0, 1, 2)
what I really want is to translate "AA(MAC1, 0, 1, 2)" to "...
0
votes
1
answer
91
views
C11 variadic macro : put elements into brackets [duplicate]
I'm looking at a macro, or more likely a combination of macros, that would achieve the following effect :
BRACKET(a) => { a }
BRACKET(a, b) => { a }, { b }
BRACKET(a, b, c) => { a }, { b ...
1
vote
1
answer
845
views
How to "map" a variadic macro with boost preprocessor?
Say I have a macro F:
#define F(x) /*...*/
and a macro G that takes one or more arguments:
#define G(...) /*...*/
and I want to write a macro H that takes one or more arguments that expands to G ...
2
votes
1
answer
173
views
Variadic macro with zero-args for ISO C++
I wrote this variadic macro template<>-maker.
#define TNAME0()
#define TNAME1(_1) typename _1
#define TNAME2(_1,_2) typename _1, typename _2
#define TNAME3(_1,_2,_3) typename _1, ...
1
vote
1
answer
931
views
How to expand a recursive macro via __VA_OPT__ in a nested context
I have read this article, which illustrates how the __VA_OPT__ function macro can be used to recursively expand a macro. I would like to implement something similar, with the difference being that the ...
2
votes
1
answer
1k
views
Is it possible to concatenate parameters of variadic macro to form a variable name?
I am trying to achieve something like the following:
#define def_name(delim, ...) ??? // how will this variadic macro concatenate its parameters to define a new variable?
// Calling `def_name` as ...
2
votes
1
answer
770
views
C header declaration for generics (macro)
I am unsure about where to write the declaration and the call of a macro that replaces the code with a function. I do not really know if I should write the macro to the .h or .c file.
Before reading ...
1
vote
1
answer
362
views
What am I doing wrong with this variadic macro in C?
I'm working on a university project and I made a macro wrapper of printf which can color the text, print the file, function and line of the instruction with a string error based on errno.
#define ...
0
votes
1
answer
1k
views
SPDLOG_LOGGER_CALL and __VA_ARGS__ in
I'm trying to understand why my variadic arguments don't work in spdlog. I understand that there is an SPDLOG_LOGGER_INFO macro to do what I do, but at the moment I need to understand how ...
2
votes
3
answers
698
views
C macro that voids variable length input arguments
Is there a way to define a macro that voids variable list of arguments?
#define VOID_ARGS(...) ((void)##__VA_ARGS__)
The use case is void arguments to suppress compiler error [-Werror=unused-value] ...
0
votes
3
answers
828
views
_Generic function with several parameters
I am using several similar functions and want to make one overloadable. The basic function takes 3 parameters, and its successive expansions take 4 or more.
#define register_read_write(action, ...
4
votes
1
answer
293
views
c++ variadic macro: how to retrieve arguments values
In the class there are a lot of methods with similar implementation, only method's name and argument-list different:
void function1 (int a, bool b)
{
mMember->function1(a, b);
}
void function2 ...