311 questions
3
votes
3
answers
486
views
How to define variadic **argv in C function
Here is the variadic example:
https://en.cppreference.com/w/c/variadic.html
I want to define two functions but I don't know how to do it correctly.
The "foo" function should accept "...
3
votes
2
answers
194
views
C macro to compute array length that returns 0 for NULL
I am writing hardcoded calls of functions that takes array(s) in parameter, e.g.
int foo(size_t length, const int array[/* length */]);
In order to avoid the error-prone process of keeping track of ...
3
votes
1
answer
168
views
Use pointer and call function in C++
There are two classes A and B, each having functions f and g (with the same signatures).
The function bool is_A(uintptr_t ptr) returns true if and only if ptr points to an instance of A, and ...
0
votes
2
answers
105
views
How do i add a secondary __VA_ARGS__?
I would like it so that the function (func parameter) can also take Variable args
#define TRY_IT(func, ret, bad_ret_code, give_up_call, ...) \
if((ret) == (bad_ret_code)) { ...
0
votes
2
answers
162
views
Optional parentheses for C variadic macro (i.e. both function-like and object-like)
Background
I have some macros of the form ATTR(XXX(...)). ATTR processes XXX into a macro ATTR_IMPL_XXX(...), and XXX can have a variable number of arguments (these variadic args are just passed to ...
1
vote
0
answers
67
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
92
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
70
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
139
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
249
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
224
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
1k
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
98
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 ...
4
votes
1
answer
2k
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
123
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, ...) ...