Questions tagged [macros]

***DO NOT USE for VBA / MS-Office languages. Use [vba] instead.*** A macro is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to an output sequence (also often a sequence of characters) according to a defined procedure.

A macro is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to an output sequence (also often a sequence of characters) according to a defined procedure. The mapping process that instantiates (transforms) a macro into a specific output sequence is known as macro expansion.

Wikipedia Article for Macro

'Macros' created in VBA/MS-Office applications, while sharing the same name, are not the same concept as defined here. Questions relating to VBA should be tagged and the respective Office-Product, e.g. , . Please see the tag wiki entry page for more information regarding proper usage.

Similarly, questions relating to other macro programming languages should not use this tag.

13505 questions
5
votes
2 answers

Scheme: difference between define and define-syntax-rule

I've been given two if-statements instructions in Racket: (define (if-fun c thn els) (if c thn els)) (define-syntax-rule (if-mac c thn els) (if c thn els)) Would someone please mind explaining the differences between how these two if-statements are…
5
votes
2 answers

C++ macro with lambda argument using 2+ captured elements generates error

foo(const std::function& functor) { .... } #define MACRO_EXAMPLE(functor) foo(functor) int main() { int i = 0, j = 0; MACRO_EXAMPLE([i](){}); // works fine MACRO_EXAMPLE([i, j](){}); // error: macro "MACRO_EXAMPLE" passed 2…
fhdnsYa
  • 443
  • 1
  • 6
  • 15
5
votes
2 answers

In Haxe, how do you add Types/Classes to a Module with macros?

I'd like to dynamically add some new Types to a given Module based on some files found in a directory. I'm essentially trying to populate a bunch of @:file(...) embed classes at the bottom of a Module. //This is the module I'm targeting to append…
chamberlainpi
  • 4,854
  • 8
  • 32
  • 63
5
votes
3 answers

Using a macro to create QObject derived classes

I'm trying to simplify (i.e. get rid of loads of boilerplate code) the creation of QObject wrapper classes that forward property access of other QObject derived classes. To start small, I'm just trying it with one property: // Sy_test.h - The…
cmannett85
  • 21,725
  • 8
  • 76
  • 119
5
votes
3 answers

What happens when a C preprocessor macro is defined twice?

I define a macro twice as follows: #define a 2 #define a 3 I thought any occurrence of a in the code would be replaced by 2, and when #define a 3 is encountered there are no more as are available in the code to be replaced by 3, so the 2 would…
Ann562
  • 71
  • 1
  • 2
5
votes
1 answer

Is semicolon ignored in C macro?

#define A; #ifdef A (...) #endif I thought the predecessor would take this as false; however it would goes into this condition. Is A and A; taken as the same?
Renjie
  • 51
  • 1
  • 4
5
votes
3 answers

How to declare instantiation in Haxe macro function

I want to create a macro that generates this code for me: if (myEntity.get(Attack) == null) myEntity.add(new Attack()); if (myEntity.get(Confused) == null) myEntity.add(new Confused()); if (myEntity.get(Defend) == null) myEntity.add(new…
Mark Knol
  • 9,663
  • 3
  • 29
  • 44
5
votes
2 answers

Using Eclipse, how do I script the formatting of a code snippet for Stack Overflow?

The background: I'm a dyed-in-the-wool Emacs user who dabbles in a lot of languages. Recently a Famous Engineer upbraided me for continuing to use Emacs in this day and age, and I wish to put this Famous Engineer's chiding to the test. (This Famous…
Owen S.
  • 7,665
  • 1
  • 28
  • 44
5
votes
1 answer

Does macrolet prohibit recursive calls among locally defined macros?

In his book ANSI Common Lisp, p.320, Paul Graham writes of macrolet: "Like flet, the local macros may not call one another." Maybe I misunderstand this but I can't think of any way in which it could be true. Macros do not call each other so much as…
user3414663
  • 531
  • 3
  • 11
5
votes
0 answers

Construct and call function programmatically

I have programmatically constructed a C function in memory, and I'm able to call it in Rust like so. type AddFn = extern "C" fn(isize, isize) -> isize; let Add = build_function::(®ion, code); fn build_function(region: &MappedRegion,…
ragingSloth
  • 1,094
  • 8
  • 22
5
votes
1 answer

Using a macro type argument inside a generated function

I'm trying to implement a macro which implements the Add trait for a struct, like so: macro_rules! implement_add { ($t:ty) => { impl std::ops::Add for $t { type Output = $t; fn add(self, rhs: $t) -> $t { …
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
5
votes
6 answers

Using C++ Macros To Check For Variable Existence

I am creating a logging facility for my library, and have made some nice macros such as: #define DEBUG myDebuggingClass(__FILE__, __FUNCTION__, __LINE__) #define WARING myWarningClass(__FILE__, __FUNCTION__, __LINE__) where myDebuggingClass and…
rcv
  • 6,078
  • 9
  • 43
  • 63
5
votes
1 answer

Is it possible to define a macro from a macro

Here is what I am thinking. #define prefix_1 1 #define prefix_2 2 #define prefix_3 3 And I want to define a macro using the prefixes above — like macro macro_prefix_1 macro_prefix_2 — and I expect them to turn into macro_1 macro_2, etc. Just like…
richard.g
  • 3,585
  • 4
  • 16
  • 26
5
votes
2 answers

C - Variadic macro which expands into set of macro calls on each argument

I want to have a single macro call which takes in multiple function pointers, and each function pointer is called by a second macro which is a function declaration. I want two macros on the form #define FUNCTION_DEF(func) extern int…
edvardsp
  • 133
  • 8
5
votes
4 answers

Given a bit mask, how to compute bit shift count

I'd like to have a function or (preferably) a macro that calculates the number of shifts required to obtain a certain bit mask. Currently I do something like: #define CURRBITMASK 0x30 #define CURRBITSHIFT 4 What I want to do: #define BITMASK1…
user2162449
  • 303
  • 2
  • 13
1 2 3
99
100