variadic-macros

is there a way to write macros with a variable argument list in visual C++?

断了今生、忘了曾经 提交于 2019-12-23 06:56:58
问题 As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...) printf(fmt); Is there a way to do that in VC++? 回答1: Yes but only since VC++ 2005. The syntax for your example would be: #define DBGPRINT(fmt, ...) printf(fmt, __VA_ARGS__) A full reference is here. 回答2: Yes, you can do this in Visual Studio C++ in versions 2005 and beyond (not sure about VS 2003). Take a look at VA_ARGS . You can basically do something like this: #define DBGPRINTF(fmt, ...) printf(fmt, __VA_ARGS_

is there a way to write macros with a variable argument list in visual C++?

半腔热情 提交于 2019-12-23 06:55:06
问题 As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...) printf(fmt); Is there a way to do that in VC++? 回答1: Yes but only since VC++ 2005. The syntax for your example would be: #define DBGPRINT(fmt, ...) printf(fmt, __VA_ARGS__) A full reference is here. 回答2: Yes, you can do this in Visual Studio C++ in versions 2005 and beyond (not sure about VS 2003). Take a look at VA_ARGS . You can basically do something like this: #define DBGPRINTF(fmt, ...) printf(fmt, __VA_ARGS_

Token pasting in C

爱⌒轻易说出口 提交于 2019-12-22 05:15:21
问题 After reading about VA_NARG I tried to implement function overloading depending on number of arguments in C using macros. Now the problem is: void hello1(char *s) { ... } void hello2(char *s, char *t) { ... } // PP_NARG(...) macro returns number of arguments :ref to link above // does not work #define hello(...) hello ## PP_NARG(__VA_ARGS__) int main(void) { hello("hi"); // call hello1("hi"); hello("foo","bar"); // call hello2("foo","bar"); return 0; } I've read this from C-faq. But still

Variadic macro with no arguments for its variadic parameter

可紊 提交于 2019-12-22 04:17:10
问题 Is it legal to invoke a variadic macro M with no arguments for its variadic parameter? The relevant standard quote is [cpp.replace]/4 : If the identifier-list in the macro definition does not end with an ellipsis, the number of arguments (including those arguments consisting of no preprocessing tokens) in an invocation of a function-like macro shall equal the number of parameters in the macro definition. Otherwise, there shall be more arguments in the invocation than there are parameters in

Can I define variadic C preprocessor macros with __VA_ARGS in the middle instead of the end?

我怕爱的太早我们不能终老 提交于 2019-12-21 12:08:59
问题 GCC complains if I do this: #define M(obj,met, ..., contents) obj##_##met(const void * self, __VA_ARGS__) { \ contents \ } Giving me these 2 reasons: error: missing ')' in macro parameter list warning: __VA_ARGS__ can only appear in the expansion of a C99 variadic macro Apparently, C99 - style variadic macros expect the closing parenthesis immediately after the ellipsis, effectively demanding that the variadic list be the last arguments of the macro. I need it to be in the middle to produce

Visual studio __VA_ARGS__ issue

Deadly 提交于 2019-12-20 15:34:04
问题 I run cl /P test.cpp, the file and result is as following. test.cpp #define FiltedLog( ...) \ if (logDetail) \ MP_LOG(LOG_INFO, __VA_ARGS__); #define MP_LOG(level,fmt,...) \ BOOAT::LOG("MP", level, fmt, ##__VA_ARGS__) #define LOG(tag,level,fmt,...) \ Log::log(tag, level, "%s: " fmt, __PRETTY_FUNCTION__, ##__VA_ARGS__) int main () { FiltedLog ( "abc", 1, 2); } Cl /P test.cpp : #line 1 "test.cpp" int main () { if (logDetail) BOOAT::Log::log("MP", LOG_INFO, "%s: " "abc", 1, 2, __PRETTY_FUNCTION_

##__VA_ARGS__ not swallowing comma when zero args under C99

十年热恋 提交于 2019-12-20 04:13:30
问题 I'd like to use a macro like the following: #define x(...) y(a,##__VA_ARGS__,b) To expand like so: x(); -> y(a,b); x(1); -> y(a,1,b); With -std=gnu99 , it works perfectly. With -std=c99 however, it looks like this: x(); -> y(a,,b); x(1); -> y(a,1,b); The ## is making no difference – it's not swallowing the comma. In other usages under C99, e.g. #define x(a,...) y(a,##__VA_ARGS__) , comma-swallowing works fine. What can I do, if anything, to get the desired comma-swallowing behaviour under

Generate multiple macro calls according to the number of arguments

我们两清 提交于 2019-12-18 09:41:38
问题 I'm trying to call this function several times in a repeditive fashion. template<class T> void METADATA_METHODS_IMPL(std::string& metadata, const T &value, const std::string &key) { metadata += boost::format("%1%:%2%") % key % value(); } I have written the following macro: #define METADATA_METHODS_IMPL_1(md, v1)\ METADATA_METHODS_IMPL(md, v1, #v1); #define METADATA_METHODS_IMPL_2(md, v1, v2)\ METADATA_METHODS_IMPL_1(md, v1)\ METADATA_METHODS_IMPL_1(md, v2) #define METADATA_METHODS_IMPL_3(md,

How to easily create fully “variadic” functions with C++ 98 standard?

我怕爱的太早我们不能终老 提交于 2019-12-18 09:37:28
问题 The library https://github.com/c42f/tinyformat/blob/2f9335afd9941688e42d60cae5166b9f0600b2d1/tinyformat.h#L1104-L1116, uses this awesome trick to do "variadic" templates on C++ 98: inline void printfln(const char* fmt) { format(std::cout, fmt); std::cout << '\n'; } template<TINYFORMAT_ARGTYPES(n)> \ void printfln(const char* fmt, TINYFORMAT_VARARGS(n)) \ { \ format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \ std::cout << '\n'; \ } I am trying to improve it by eliminating the requirement to

c++ macro to import all names of base template class [duplicate]

霸气de小男生 提交于 2019-12-14 03:56:51
问题 This question already has answers here : Is it possible to iterate over arguments in variadic macros? (9 answers) Closed 7 months ago . When deriving a class from a template base class, one is forced to use the syntax this->member , or using Base::member to get access to a member of the base class. The same would happen if the base class is a generic template parameter and one is sure that some members exists. Would it be possible to write a macro that "import" all members of the base class