macros

macro dependent macro

半世苍凉 提交于 2020-01-14 09:21:09
问题 Is it possible to do something like this: #define F(x) \ #ifdef DOUBLE \ 2*x \ #else \ x \ #endif so that when I use F , what it expands to depends on whether the macro DOUBLE is defined? I don't think so, but I'm hopeful. GNU expansions are fine. Edit In response to some of the answers, I'm really using this to do some code generation, where the code is slightly different depending on where it gets defined. Because of the order in which some files are included and where the relevant macros

In clojure, how to do code templating when implementing a macro using recursion

我的梦境 提交于 2020-01-14 09:16:50
问题 I am trying to implement a macro to recursively converting an infix list into a prefix one. I encounter a problem as follows: ;;this works (defmacro recursive-infix [form] (list (second form) (first form) (if (not (seq? (nth form 2))) (nth form 2) (recursive-infix (nth form 2))))) ;;this doesn't work (defmacro my-recursive-infix [form] `(~(second form) ~(first form) (if (not (seq? ~(nth form 2))) ~(nth form 2) (my-recursive-infix ~(nth form 2))))) (macroexpand '(recursive-infix (10 + 10))) ;

In clojure, how to do code templating when implementing a macro using recursion

拜拜、爱过 提交于 2020-01-14 09:16:47
问题 I am trying to implement a macro to recursively converting an infix list into a prefix one. I encounter a problem as follows: ;;this works (defmacro recursive-infix [form] (list (second form) (first form) (if (not (seq? (nth form 2))) (nth form 2) (recursive-infix (nth form 2))))) ;;this doesn't work (defmacro my-recursive-infix [form] `(~(second form) ~(first form) (if (not (seq? ~(nth form 2))) ~(nth form 2) (my-recursive-infix ~(nth form 2))))) (macroexpand '(recursive-infix (10 + 10))) ;

How can I use a macro for collecting variable names?

不羁岁月 提交于 2020-01-14 04:34:48
问题 I would like to simplify the following class A { int a; int b; int c; std::vector<int*> addrs; public: A() : addrs{ &a, &b, &c } {} }; so that I don't have the write the list of fields in two places, i.e. the declarations and the initializer for addrs . Is there some way to use a macro to collect the declarations and use them later. E.g., class A { VAR_DECL(a); VAR_DECL(b); VAR_DECL(c); std::vector<int*> addrs; public: A() : addrs{ VAR_ADDRESSES } {} }; For context this is intended for

How to know whether a symbol represents function or macro?

血红的双手。 提交于 2020-01-14 02:28:10
问题 I'm writing a macro for function / macro composition (mixed combinations are possible). Inside of the macro I have to treat symbols which represent functions and those that name macros differently. It is because result function must work with any number of arguments (if 'lowest' function in composition can), and I cannot apply apply to macros. My question: how to determine what a given symbol represents: function or macro? 回答1: Macro: CL-USER 8 > (macro-function 'bar) NIL CL-USER 9 > (macro

Variadic heterogenous FREE macro

孤人 提交于 2020-01-14 01:37:38
问题 I want a macro to free multiple (variadic number) pointers of different type . Based on similar questions in SO I made this code which seems to work #include <stdio.h> #include <stdarg.h> #include <stdlib.h> /* your compiler may need to define i outside the loop */ #define FREE(ptr1, ...) do{\ void *elems[] = {ptr1, __VA_ARGS__};\ unsigned num = sizeof(elems) / sizeof(elems[0]);\ for (unsigned i=0; i < num; ++i) free(elems[i]);\ } while(0) int main(void) { double *x = malloc(sizeof(double));

Variadic heterogenous FREE macro

隐身守侯 提交于 2020-01-14 01:37:27
问题 I want a macro to free multiple (variadic number) pointers of different type . Based on similar questions in SO I made this code which seems to work #include <stdio.h> #include <stdarg.h> #include <stdlib.h> /* your compiler may need to define i outside the loop */ #define FREE(ptr1, ...) do{\ void *elems[] = {ptr1, __VA_ARGS__};\ unsigned num = sizeof(elems) / sizeof(elems[0]);\ for (unsigned i=0; i < num; ++i) free(elems[i]);\ } while(0) int main(void) { double *x = malloc(sizeof(double));

are there problems with this macro - check status and return if failed

核能气质少年 提交于 2020-01-13 18:10:51
问题 we have a frequently recurring lines in the code that check for the return status of a function, and if it's a failure return with the status immediately. I thought of defining a macro for this: #define RETURN_IF_FAILED(x) { int stat = (x); if (FAILED(stat)) return stat; } The local variable is because the parameter x could be a function call. As I know there are some weird artifacts of using macros and I am not very proficient in them, I'd like to ask if you see any problems with this macro.

C++ Shorten subsequent function calls

六月ゝ 毕业季﹏ 提交于 2020-01-13 17:56:40
问题 I try to find a way in C++17 to shorten following function calls: GetCurrentContext()->GetEntityManager()->CreateEntity(); maybe to something like: EM()->CreateEntity(); I tried a function pointer, but this is only valid for static functions: constexpr auto &EM = GetContext()->GetEntityManager; // error: reference to non-static member function must be called; Is there a better solution than using a Macro? #define EM GetContext()->GetEntityManager When using an inline function call, i'm afraid

comma (,) in C Macro Definition

巧了我就是萌 提交于 2020-01-13 13:13:06
问题 I've never seen this syntax before. #define SNS(s) (s),(sizeof(s)-1) The way i'm reading this is that SNS(s) = sizeof(s)-1 . What is the comma doing? Is it necessary? int ft_display_fatal(const char *err, unsigned len, int fd, int rcode) { UNUSED(write(fd, err, len)); return (rcode); } Main return (ft_display_fatal(SNS("File name missing.\n"), 2, 1)); 回答1: Macros are just text replacement, so they can expand to just about anything you want. In this case, the macro is being used to expand into