macros

how can I avoid the use of #if in a polymorphic print macro

◇◆丶佛笑我妖孽 提交于 2020-04-07 08:01:55
问题 Let's try to run the following code: #include <stdio.h> #define MY_MACRO1(isArray,y) do { \ if(isArray) \ printf("%d", y[0]); \ else \ printf("%d", y); \ }while(0) int main() { int a = 38; int b[]={42}; MY_MACRO1(0,a); return 0; } it returns the error: main.c: In function ‘main’: main.c:12:39: error: subscripted value is neither array nor pointer nor vector printf("%d", y[0]); \ Ok, so we would need a #if statement to run y[0] only if the variable is an array: #define MY_MACRO2(isArray,y) do

how can I avoid the use of #if in a polymorphic print macro

℡╲_俬逩灬. 提交于 2020-04-07 08:01:27
问题 Let's try to run the following code: #include <stdio.h> #define MY_MACRO1(isArray,y) do { \ if(isArray) \ printf("%d", y[0]); \ else \ printf("%d", y); \ }while(0) int main() { int a = 38; int b[]={42}; MY_MACRO1(0,a); return 0; } it returns the error: main.c: In function ‘main’: main.c:12:39: error: subscripted value is neither array nor pointer nor vector printf("%d", y[0]); \ Ok, so we would need a #if statement to run y[0] only if the variable is an array: #define MY_MACRO2(isArray,y) do

How to use static assert in C to check the types of parameters passed to a macro

不打扰是莪最后的温柔 提交于 2020-04-05 05:28:12
问题 I need to write a C macro that checks to ensure all parameters passed to it are unsigned and of the same integer type. Ex: all input params are uint8_t , or all uint16_t , or all uint32_t , or all uint64_t . Here is how this type of checking can be done in C++: Use static_assert to check types passed to macro Does something similar exist in C, even if only by way of a gcc extension? Note that static asserts are available in gcc via _Static_assert . (See my answer here: Static assert in C).

C++ Macro to replace text with nothing

泪湿孤枕 提交于 2020-03-26 05:42:09
问题 Macros are often used for text substitution. In my case, I need to conditionally wipe out some keywords, so that compilation will be possible in compilers that don't have the specific feature. Specifically I've been looking into cpp11 range where this snippet comes from template <typename C> struct has_size { template <typename T> static constexpr auto check(T*) -> // problem in VS2013 typename std::is_integral< decltype(std::declval<T const>().size())>::type; // .. some more stuff }; I'm

Assign an array or an integer without knowing its nature in the function code (but compiler knows)

帅比萌擦擦* 提交于 2020-03-24 14:15:50
问题 I'm looking for something like this snippet. I expect it to know, at compile time, wether it is dealing with an array or not, and avoid the following errors. #include <stdio.h> #define IS_ARRAY(x,type) _Generic((&x), \ type (*)[]: 1, \ default: 0) #define GENERIC_ASSIGN(arg,type) if(IS_ARRAY(arg,type)){arg[0] = 1; arg[1] = 2;}else{arg = 2;} int main(void) { int foo = 0; int bar[10] = {0}; GENERIC_ASSIGN(bar,int); //--> error: assignment to expression with array type GENERIC_ASSIGN(foo,int); /

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

Windows Task Manager Schedule VBA Macro to Send An Email Using OutLook Daily Running Manually and Not Automatically

拈花ヽ惹草 提交于 2020-03-03 10:11:09
问题 I have my macro which is running, & for this I created a vb script and used windows task manager to schedule its run every day. Whenever I run manually or attempt changing the time in the trigger, I always make sure that both excel and outlook are not running. If I run the macro in Excel VBA, it sends the email. After scheduling the task to run everyday, just as a test if I go to (in TaskScheduler) View -> Hidden Tasks and manually click Run, it sends the email. However, if I schedule it to

Alternatives to stringifying the variable name in C++11

旧街凉风 提交于 2020-03-03 07:57:21
问题 In my code, I have repeatedly this expression: T foo; do_sth(foo, "foo"); I am considering stringifying the variable name, like this: #define VARNAME(Var) (#Var) void do_sth_new(T foo) { do_sth(foo, VARNAME(foo)); }; T foo; do_sth_new(foo); Is it good practice? Is there any better alternative in C++11? 回答1: As you show it, it doesn't work since VARNAME(foo) will always be "foo" (as this is the parameter's name). You have to write do_sth_new itself as macro: #define do_sth_new(_foo) \ do { do

Why one needs two brackets to use macros in c/c++?

﹥>﹥吖頭↗ 提交于 2020-02-27 09:18:32
问题 KdPrint(("Enter HelloWDMAddDevice\n")); What's the reason for doing that? 回答1: That is so you can pass an entire argument list to the macro and have it pass it on to a function that takes a variable number of arguments. I would bet anything that the definition of that macro is: #if DEBUG /* or something like it */ #define KdPrint(args) (printf args) #else #define KdPrint(args) /* empty */ #endif Or similar to some other function that works just like printf. If it were defined as printf(args),