variadic-macros

Variadic Macro: cannot pass objects of non-trivially-copyable type through '…'

你说的曾经没有我的故事 提交于 2020-03-20 06:41:05
问题 I am trying to write a macro for logging mechanism. I wrote a variadic macro but it does not work with std::string . The code looks like the following: #include <stdio.h> #include <string> #define LOG_NOTE(m, ...) printf(m, ##__VA_ARGS__) int main() { std::string foo = "random string"; int bar = 5; LOG_NOTE("%s %d %s", "Hello World", bar, foo); return 0; } If I would call the macro like following, I would not get any error. LOG_NOTE("%s %d %s", "Hello World", bar, "random string"); Compiler

Variadic Macro: cannot pass objects of non-trivially-copyable type through '…'

坚强是说给别人听的谎言 提交于 2020-03-20 06:40:08
问题 I am trying to write a macro for logging mechanism. I wrote a variadic macro but it does not work with std::string . The code looks like the following: #include <stdio.h> #include <string> #define LOG_NOTE(m, ...) printf(m, ##__VA_ARGS__) int main() { std::string foo = "random string"; int bar = 5; LOG_NOTE("%s %d %s", "Hello World", bar, foo); return 0; } If I would call the macro like following, I would not get any error. LOG_NOTE("%s %d %s", "Hello World", bar, "random string"); Compiler

Comma in C/C++ macro passed to another macro

无人久伴 提交于 2020-02-04 04:43:10
问题 I have these macros which generate error in Visual Studio 2015. #define log_params __FILE__, __LINE__ #define log(file, line, message, ...) _snprintf_s(nullptr, 0, 0, message, __VA_ARGS__) Now calling this never works log(log_params, "testing %d", 4) Any thoughts? I also checked output of preprocessor and it is: _snprintf_s(nullptr, 0, 0, 4 ); EDIT 1 Intresting finding #define log(file, line, message, ...) file line will produce this : "service.cpp", 164 "testing %d" Is it normal? 回答1: The

C preprocessor macro doesn't parse comma separated tokens?

白昼怎懂夜的黑 提交于 2020-01-24 20:21:29
问题 I want to choose one of two functions depending on the number of arguments: nargs = 0 ----> f1 nargs > 0 ----> f2. Macros do the following: get the first argument, then if no argument supplied ,it would add two commas " ,NULL,NULL ". Then it would select the second argument from the returned list of arguments. for example: f("Hello, world%i%s", x , s) ----> " Hello, world%i%s " ----> void f() ----> ,NULL,NULL ----> NULL so I can get null or void depending the on number of arguments. Here is

C preprocessor macro doesn't parse comma separated tokens?

六月ゝ 毕业季﹏ 提交于 2020-01-24 20:20:07
问题 I want to choose one of two functions depending on the number of arguments: nargs = 0 ----> f1 nargs > 0 ----> f2. Macros do the following: get the first argument, then if no argument supplied ,it would add two commas " ,NULL,NULL ". Then it would select the second argument from the returned list of arguments. for example: f("Hello, world%i%s", x , s) ----> " Hello, world%i%s " ----> void f() ----> ,NULL,NULL ----> NULL so I can get null or void depending the on number of arguments. Here is

Variadic macros with zero arguments, and commas

房东的猫 提交于 2020-01-24 02:21:07
问题 Consider this macro: #define MAKE_TEMPLATE(...) template <typename T, __VA_ARGS__ > When used with zero arguments it produces bad code since the compiler expects an identifier after the comma. Actually, VC's preprocessor is smart enough to remove the comma, but GCC's isn't. Since macros can't be overloaded, it seems like it takes a separate macro for this special case to get it right, as in: #define MAKE_TEMPLATE_Z() template <typename T> Is there any way to make it work without introducing

How to expand macro and delete comma

倖福魔咒の 提交于 2020-01-23 02:04:20
问题 For example I want to write my own printf() alternative, but I have to perform calculations on the variable arguments: #define log(fmt_string, ...) my_log(fmt_string, pack_args(__VA_ARGS__), __VA_ARGS__) where pack_args(...) - is a macro too. How should I change this code to handle the only fmt_string presence scenario? log("Some message here"); 回答1: In P99 I have two macros #define P00_ARG( \ _1, _2, _3, _4, _5, _6, _7, _8, \ _9, _10, _11, _12, _13, _14, _15, _16, \ ... etc ... \ _153, _154,

How to expand macro and delete comma

99封情书 提交于 2020-01-23 02:04:05
问题 For example I want to write my own printf() alternative, but I have to perform calculations on the variable arguments: #define log(fmt_string, ...) my_log(fmt_string, pack_args(__VA_ARGS__), __VA_ARGS__) where pack_args(...) - is a macro too. How should I change this code to handle the only fmt_string presence scenario? log("Some message here"); 回答1: In P99 I have two macros #define P00_ARG( \ _1, _2, _3, _4, _5, _6, _7, _8, \ _9, _10, _11, _12, _13, _14, _15, _16, \ ... etc ... \ _153, _154,

How to stringify a string which contains a comma?

╄→尐↘猪︶ㄣ 提交于 2020-01-22 16:00:29
问题 I want to pass a version string in the compile command: $ g++ -Wall -D VERSION="2013-12-03 02:15:21, commit cb060df" -o main main.cpp Inside my code, I have the following: #define TOSTR_(x) #x #define STRINGIFY(x) TOSTR_(x) #define VERSION_STR STRINGIFY(VERSION) This doesn't work, because the VERSION macro has a comma in it, so it looks like I'm passing two arguments to TOSTR() (apparently, the VERSION macro only gets expanded after it's passed to STRINGIFY() as one unique argument). The

variadic arguements and x64

笑着哭i 提交于 2020-01-20 08:39:26
问题 Well I've found something that interests me and I didn't manage to find an answer for it.... how does the va_arg \ va_start \ va_list \ va_end macros work under the hood in x64? calling convention in i386 passes parameters on the stack hence the macro just increments some pointer that points to the stack base and forwards it however in x64 all parameters are passed by registers.... so what happens there? how does the called function know which registers were used to pass arguments and doesn't