variadic-macros

How to single out the first parameter sent to a macro taking only a variadic parameter

孤街浪徒 提交于 2019-12-12 11:13:27
问题 I try to get at the first actual parameter sent to a variadic macro. This is what I tried, and which does not work in VS2010: #define FIRST_ARG(N, ...) N #define MY_MACRO(...) decltype(FIRST_ARG(__VA_ARGS__)) When I look at the preprocessor output I see that FIRST_ARG returns the entire argument list sent to MY_MACRO ... On the other hand when I try with: FIRST_ARG(1,2,3) it expands to 1 as intended. This seems to be somehow the inverse of the problem solved by the infamous two level concat

Error when defining a stringising macro with __VA_ARGS__

强颜欢笑 提交于 2019-12-12 08:26:59
问题 I have been trying to implement a function macro in C that prepends "DEBUG: ", to the argument, and passes its arguments to printf: #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) This gives me this error in gcc: src/include/debug.h:4:70: error: expected expression before ‘)’ token #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) ^ Supposedly, it should stringise format, and pass its variable arguments to printf, but so far I can't get past this error.

Is va_start required in variadic arguments for functions?

独自空忆成欢 提交于 2019-12-12 01:46:23
问题 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 is no va_start ? static void outputError(Boolean useErr, int err, Boolean flushStdout, const char *format, va_list ap) { #define BUF_SIZE 500 char buf[BUF_SIZE], userMsg[BUF_SIZE], errText[BUF_SIZE]; vsnprintf(userMsg, BUF_SIZE, format, ap); if

Is there an easier way to do a macro to define a function with variable amount of arguments?

北慕城南 提交于 2019-12-11 18:47:34
问题 I have a macro that defines a function with a variable amount of arguments, the macro has some logic to decide which real function must be called. My current approach is the following: #define FUNC(ret,args,args_call) \ ret my_func(args) { \ if( something ) other_func(args_call);\ return one_func(args_call);\ } #define PARAM(...) __VA_ARGS__ I use it like that: class AClass : public AInterface { public: FUNC(int,PARAM(int a, int b),PARAM(a,b)) }; I was wondering if there is a better way to do

How to fix variadic macro related issues with “macro overloading” in MSVC++ (Microsoft Visual studio)? [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-11 15:37:38
问题 This question already has answers here : MSVC++ variadic macro expansion (2 answers) Closed last year . Inspired from this kind of solution, I have written below code, which simulates "overloading of macros" . #include<iostream> #define CONCATE_(X,Y) X##Y #define CONCATE(X,Y) CONCATE_(X,Y) #define UNIQUE(NAME) CONCATE(NAME, __LINE__) #define NUM_ARGS_(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, TOTAL, ...) TOTAL #define NUM_ARGS(...) NUM_ARGS_(__VA_ARGS__, 14, 13, 12, 11, 10,

Variadic macro expanding

ε祈祈猫儿з 提交于 2019-12-11 14:00:27
问题 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 <stdio.h> #define _VA_NARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N #define _VA_NARGS(...) _VA_NARGS_IMPL(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1) #define binder(count, ...) arg##count(__VA_ARGS__) #define foo(...) binder(_VA_NARGS(__VA_ARGS__), __VA_ARGS__) #define arg1(_1) _1 #define arg2(_1, _2) _1, _2 #define arg3(_1, _2, _3) _1, _2, _3 int main() { printf("%d %d

Argument counting in macro

时间秒杀一切 提交于 2019-12-11 03:34:43
问题 I'm trying to understand the argument counting in C preprocessing macro and the idea in this answer. We have the following macro (I changed the number of arguments for simplicity): #define HAS_ARGS(...) HAS_ARGS_(__VA_ARGS__, 1, 1, 0,) #define HAS_ARGS_(a, b, c, N, ...) N As far as I understand the purpose of this macro is to check if the given varargs empty. So on empty varargs the macro invokation is replaced with 0 which seems fine. But with a single argument it also turns into 0 which I

Counting function arguments at compile time

断了今生、忘了曾经 提交于 2019-12-10 16:38:47
问题 I'm trying to count the number of arguments to a function at compile time (I'm wrapping sprintf up in some templates for compile time checks and type safety). I need to check that the number of arguments matches the number of formatting placeholders at compile time. A first pass at this is pretty simple: template <typename... Args> constexpr u32 CountArgs(Args&&... args) { return sizeof...(args); } constexpr u32 CountFormatSpecifiers(c8* format); template <typename... Args> c8* String

__VA_ARGS__ expansion using MSVC

爷,独闯天下 提交于 2019-12-09 17:14:21
问题 I found a question showing how to overload macros based on the number of arguments : Overloading Macro on Number of Arguments But as they say, it's not working using MSVC because MSVC expands __VA_ARGS__ into a single token instead of a list of arguments ( arg1, arg2, arg3 ). They point to another question where a work around is given : MSVC doesn't expand __VA_ARGS__ correctly But not explained at all, so I can't adapt it to my own case since I can't understand it. Could you please explain

Converting a variadic macro to a variadic template function?

喜夏-厌秋 提交于 2019-12-08 06:54:53
问题 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__); \ } else { \ throw invalid_function_id(FId); \ } \ } \ /**/ -- how can this be rewritten to a variadic function template? template<typename FType, typename ...Args> /*return type?*/ tmpl_call_return_f(MyFunId const& FId, /*what goes here?*/) { ... FType f = (FType)GetFuncFomId(FId); return f(/*what goes here?*/); ...