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
79
votes
3 answers

What are the implications of the linux __user macro?

I was hoping someone could explain the nuances of the __user macro used in the linux kernel source. First of all, the macro: # define __user __attribute__((noderef, address_space(1))) Now, after some googling I read that this macro allows…
Mr. Shickadance
  • 5,283
  • 9
  • 45
  • 61
79
votes
6 answers

Can we have recursive macros?

I want to know if we can have recursive macros in C/C++? If yes, please provide a sample example. Second thing: why am I not able to execute the below code? What is the mistake I am doing? Is it because of recursive macros? # define pr(n) ((n==1)? 1…
user1367292
  • 1,029
  • 2
  • 11
  • 13
77
votes
5 answers

How to use a Objective-C #define from Swift

I am migrating a UIViewController class to train a bit with Swift. I am successfully using Objective-C code via the bridging header but I have the need of importing a constants file that contains #define directives. I have seen in Using Swift with…
atxe
  • 5,029
  • 2
  • 36
  • 50
77
votes
10 answers

Array-size macro that rejects pointers

The standard array-size macro that is often taught is #define ARRAYSIZE(arr) (sizeof(arr) / sizeof(arr[0])) or some equivalent formation. However, this kind of thing silently succeeds when a pointer is passed in, and gives results that can seem…
nneonneo
  • 171,345
  • 36
  • 312
  • 383
73
votes
5 answers

The use of double include guards in C++

So I recently had a discussion where I work, in which I was questioning the use of a double include guard over a single guard. What I mean by double guard is as follows: Header file, "header_a.hpp": #ifndef __HEADER_A_HPP__ #define…
sh3rifme
  • 1,054
  • 1
  • 11
  • 26
72
votes
5 answers

Any utility to test expand C/C++ #define macros?

It seems I often spend way too much time trying to get a #define macro to do exactly what i want. I'll post my current dilemma below and any help is appreciated. But really the bigger question is whether there is any utility someone could recommend,…
Randy
  • 751
  • 1
  • 6
  • 5
72
votes
1 answer

Documenting Scala 2.10 macros

I'll start with an example. Here's an equivalent of List.fill for tuples as a macro in Scala 2.10: import scala.language.experimental.macros import scala.reflect.macros.Context object TupleExample { def fill[A](arity: Int)(a: A): Product = macro…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
71
votes
3 answers

How to allow optional trailing commas in macros?

Here's a synthetic example of what I want: macro_rules! define_enum { ($Name:ident { $($Variant:ident),* }) => { pub enum $Name { None, $($Variant),*, } } } define_enum!(Foo { A, B }); This code…
user1244932
  • 7,352
  • 5
  • 46
  • 103
69
votes
2 answers

*args, **kwargs in jinja2 macros

How are extra args & kwargs handled for a Jinja2 macro? The documentation isn't exactly clear offhand. For example, this is clearly wrong: {% macro example_1(one, two, **kwargs) %} do macro stuff {% endmacro %} which results…
blueblank
  • 4,724
  • 9
  • 48
  • 73
65
votes
3 answers

In a GNU C macro envSet(name), what does (void) "" name mean?

I came across this syntax today and couldn't work out what it meant: // Uses the GNU C statement expression extension #define envSet(name) ({ \ static int initialised; \ static bool set; \ (void) "" name; \ if (!initialised || !g_cacheEnv) { \ …
OMGtechy
  • 7,935
  • 8
  • 48
  • 83
65
votes
4 answers

Why is this macro replaced as 20 instead 10?

1. #define NUM 10 2. #define FOO NUM 3. #undef NUM 4. #define NUM 20 5. 6. FOO When I only run the preprocessor, the output file contains 20. However, from what I understand, the preprocessor simply does text replacement. So this is what I think…
OneZero
  • 11,556
  • 15
  • 55
  • 92
65
votes
8 answers

Macros in Swift?

Does Swift currently support macros, or are there future plans to add support? Currently I'm scattering: Log.trace(nil, function: __FUNCTION__, file: __FILE__, line: __LINE__) In various places throughout my code.
jhurliman
  • 1,790
  • 1
  • 18
  • 20
65
votes
25 answers

Are C++ Templates just Macros in disguise?

I've been programming in C++ for a few years, and I've used STL quite a bit and have created my own template classes a few times to see how it's done. Now I'm trying to integrate templates deeper into my OO design, and a nagging thought keeps coming…
Roddy
  • 66,617
  • 42
  • 165
  • 277
64
votes
2 answers

What does Q_D macro do in Qt

I am exploring Qt source code and came across this Q_D macro. Could some one please explain what it does? Almost all the time it should return a widget of the type given as a parameter which is the d variable. Need more clarification on this.
Tharanga
  • 2,007
  • 4
  • 32
  • 50
63
votes
6 answers

How to undefine a define at commandline using gcc

How do I at compile time undefine a compiler macro using gcc. I tried some compile args to gcc like -D but I can't get to see the "not defined" message. Thanks #include #define MYDEF int main(){ #ifdef MYDEF …
monkeyking
  • 6,670
  • 24
  • 61
  • 81