macros

Why is the return value not what I expected in this C program with macro? [duplicate]

こ雲淡風輕ζ 提交于 2020-01-30 11:40:06
问题 This question already has answers here : Strange behaviour of macros C/C++ (5 answers) Strange behavior of Macro-expansion (3 answers) Closed 6 years ago . When I run the following code, the return value is 11, but I was expecting it to return 25. Can someone explain this? #include<stdio.h> #define SQR(a) a*a int main() { int i=3; printf("%d",SQR(i+2)); return 1; } 回答1: Needs more parentheses. This: #define SQR(a) a*a expands to this: i+2*i+2 which is: 3+2*3+2 which is 11 because * has

How to extract __VA_ARGS__?

怎甘沉沦 提交于 2020-01-29 18:55:08
问题 I hava a macro to call static function for each args. For example: #define FOO(X) X::do(); #define FOO_1(X,Y) X::do(); Y::do(); My question is that I need to use foo with variable number of arguments, is it possible to use __VA_ARGS__ ? Like the line below: #define FOO(...) __VA_ARGS__::do() ? Thanks 回答1: Macro expansion does not work like argument pack expansion with variadic templates. What you have will expand to: X,Y::do(); And not to X::do(); Y::do(); As you hoped. But in C++11 you could

#define inside an enum or how to extend an enum

江枫思渺然 提交于 2020-01-25 10:56:05
问题 I have several classes each of them uses the same enum but extends it a bit, based on its requirements. For example : class skirtColor{ enum Color{ red = 1, blue = 10, green = 12 }; }; class dressColor { enum Color{ red = 1, pink, yellow, blue = 10, green = 12 }; }; class pantsColor { enum Color { red = 1, brown, blue = 10, green = 12 }; }; Since there is no inheritance for enum in C++, I would like to use define for a common part #define COLOR\ // red color \ red = 1,\ // blue color \ blue =

How to return focus to editor in VS Code macro sending Python text to Debug Console?

孤街醉人 提交于 2020-01-25 07:15:12
问题 I have tried to key-bind a macro to send python text to the Debug Console and return focus to the editor in Visual Studio Code. This is what I have tried: Installed the vscode-python extension Installed the macros extension settings.json : { "macros": { "selectionToReplAndReturnToEditor": [ "editor.debug.action.selectionToRepl", "workbench.action.focusActiveEditorGroup" ] } } keybindings.json : [ { "key": "alt+f9", "command": "workbench.action.focusActiveEditorGroup", }, { "key": "alt+f10",

How to compose stringification with user defined literal (UDL) in Macro?

自作多情 提交于 2020-01-25 06:52:29
问题 How to use literal suffix for identifier transformed into literal string in MACRO by #identifier ? struct SomeType; SomeType operator "" _udl(const char* self); #define STRINGIFY_AS_UDL(id) /* #id _udl doesn't work */ /* How to have "id"_udl */ STRINGIFY_AS_UDL(foo) // -> "foo"_udl STRINGIFY_AS_UDL(bar) // -> "bar"_udl STRINGIFY_AS_UDL(42) // -> "42"_udl 回答1: UDL operators are also "regular" functions, so you can call them instead: #define STRINGIFY_AS_UDL(id) operator ""_udl(#id) but you can

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

How do you compare variable types in assembly?

混江龙づ霸主 提交于 2020-01-24 08:38:28
问题 This might be a bit of a stupid syntax question, but is there a way you can make conditional jumps based on variable type? I'm trying to write a macro (for a class) that can take a byte, word, or double word as an argument and write it to the screen. mWriteInt MACRO integer:REQ ;cmp integer, DWORD ;je dwordOp movsx eax, word ptr integer call WriteInt mov edx, OFFSET endl call WriteString ; for a DWORD ; dwordOp: ENDM So basically, the code executed should be different based on what type of

How do you compare variable types in assembly?

眉间皱痕 提交于 2020-01-24 08:38:05
问题 This might be a bit of a stupid syntax question, but is there a way you can make conditional jumps based on variable type? I'm trying to write a macro (for a class) that can take a byte, word, or double word as an argument and write it to the screen. mWriteInt MACRO integer:REQ ;cmp integer, DWORD ;je dwordOp movsx eax, word ptr integer call WriteInt mov edx, OFFSET endl call WriteString ; for a DWORD ; dwordOp: ENDM So basically, the code executed should be different based on what type of

LISP: how to trace macros

…衆ロ難τιáo~ 提交于 2020-01-24 06:37:09
问题 This is probably a stupid question, but I'm walking through the PG lisp book, and I wanted to step through some example macros that he provides with actual values, for instance: (defmacro our-let (binds &body body) `( (lambda ,( mapcar #'(lambda (x) (if (consp x) (car x) x)) binds ) ,@body ) ,@(mapcar #'(lambda (x) (if (consp x) (cadr x) nil)) binds) ) ) I naively tried to run (trace our-let) and then (our-let ((x 1) (y 2)) (+ x y)) but I'm getting an error, can't use encapsulation to trace