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.

macros
Filter by
Sorted by
Tagged with
43 votes
2 answers
69k views

Add preprocessor macro to a target in xcode 6

Probably this is pretty simple, but I can't find a way to define a preprocessor macro for a target in Xcode 6.
user1051307's user avatar
43 votes
1 answer
13k views

What does the tt metavariable type mean in Rust macros?

I'm reading a book about Rust, and start playing with Rust macros. All metavariable types are explained there and have examples, except the last one – tt. According to the book, it is a “a single ...
Pavel Davydov's user avatar
43 votes
2 answers
54k views

Expand macros inside quoted string [duplicate]

Possible Duplicate: C Macros to create strings I have a function which accepts one argument of type char*, like f("string"); If the string argument is defined by-the-fly in the function call, ...
davide's user avatar
  • 2,142
43 votes
9 answers
56k views

macro definition containing #include directive

Is there a way to define a macro that contains a #include directive in its body? If I just put the "#include", it gives the error C2162: "expected macro formal parameter" since here I am not using #...
Bing Jian's user avatar
  • 1,024
43 votes
7 answers
14k views

When was the NULL macro not 0?

I vaguely remember reading about this a couple of years ago, but I can't find any reference on the net. Can you give me an example where the NULL macro didn't expand to 0? Edit for clarity: Today it ...
György Andrasek's user avatar
43 votes
6 answers
21k views

Python-like C++ decorators

Are there ways to decorate functions or methods in C++ like in python style? @decorator def decorated(self, *args, **kwargs): pass Using macros for example: DECORATE(decorator_method) int ...
Artem Selivanov's user avatar
43 votes
3 answers
30k views

Can you capitalize a pasted token in a macro?

In a C macro, is it possible to capitalize a pasted-in token? For example, I currently have the following macro: #define TEST(name, keyword) \ test_##name: TEST_##keyword##_KEYWORD I ...
DuneBug's user avatar
  • 1,563
42 votes
4 answers
42k views

Macros for GCC/G++ to differentiate Linux and Mac OSX?

Macros for GCC/G++ to differentiate Linux and Mac OSX?
Viet's user avatar
  • 18.1k
42 votes
5 answers
42k views

Is it possible to place a macro in a namespace in c++?

My application uses another output than the standard output for logging information, which is why I wrote my own Log(), Error(), Panic() and Assert() functions. To organize things nicely, I enclose ...
Tibi's user avatar
  • 3,835
42 votes
1 answer
4k views

Static return type of Scala macros

So I've got this macro: import language.experimental.macros import scala.reflect.macros.Context class Foo class Bar extends Foo { def launchMissiles = "launching" } object FooExample { def foo: ...
Travis Brown's user avatar
41 votes
8 answers
43k views

The need for parentheses in macros in C [duplicate]

I tried to play with the definition of the macro SQR in the following code: #define SQR(x) (x*x) int main() { int a, b=3; a = SQR(b+5); // Ideally should be replaced with (3+5*5+3), ...
Kushal's user avatar
  • 3,130
41 votes
1 answer
12k views

What is "with-eval-after-load" in Emacs Lisp

I came across the macro with-eval-after-load when trying to install persp-mode from here. But I am unable to find the macro inside Emacs and/or on Google. Where is it defined? Is it part of standard ...
Talespin_Kit's user avatar
  • 21.3k
41 votes
2 answers
5k views

Where can I learn about constructing AST's for Scala macros?

Where I can learn how to construct the AST's that Scala's macros generate? The Scaladoc isn't as helpful as I'd like. For example: abstract def Apply(sym: Universe.Symbol, args: Universe.Tree*): ...
Bill's user avatar
  • 45.4k
41 votes
3 answers
20k views

How to prefix/suffix identifiers within a macro? [duplicate]

When using a macro that defines a function, is it possible to add a prefix to the function? macro_rules! my_test { ($id:ident, $arg:expr) => { #[test] fn $id() { ...
ideasman42's user avatar
40 votes
3 answers
79k views

C macro: #if check for equality

Is there a way to do check for numerical equality in macros? I want to do something like #define choice 3 #if choice == 3 .... #endif #if choice == 4 ... #endif Does C macros have support for ...
anon's user avatar
  • 41.8k
40 votes
9 answers
34k views

Inline function v. Macro in C -- What's the Overhead (Memory/Speed)?

I searched Stack Overflow for the pros/cons of function-like macros v. inline functions. I found the following discussion: Pros and Cons of Different macro function / inline methods in C ...but it ...
Jason R. Mick's user avatar
39 votes
5 answers
93k views

How do you implement header guards, and what can you put between them?

At LearnCpp.com | 1.10 — A first look at the preprocessor. Under Header guards, there are those code snippets: add.h: #include "mymath.h" int add(int x, int y); subtract.h: #include "...
Simplicity's user avatar
  • 48.2k
39 votes
9 answers
154k views

How to write macro for Notepad++?

I would like to write a macro for Notepad++ which should replace char1, char2, char3 with char4, char5, char6, respectively.
Igor Drincic's user avatar
  • 1,975
39 votes
1 answer
18k views

How does Q_FOREACH (= foreach) macro work and why is it that complex?

In Qt, there is a foreach loop which is implemented using macros (Q_FOREACH). There are different implementations, depending on the compiler. The definition for GCC is as follows: #define Q_FOREACH(...
leemes's user avatar
  • 45.4k
39 votes
3 answers
44k views

Is there a macro definition to check the Linux kernel version?

I'm wondering if there is a gcc macro that will tell me the Linux kernel version so I can set variable types appropriately. If not, how would I go about defining my own macro that does this?
zztops's user avatar
  • 694
39 votes
6 answers
72k views

Is there a way to both check a macro is defined and it equals a certain value at the same time

I regularly use object-like preprocessor macros as boolean flags in C code to turn on and off sections of code. For example #define DEBUG_PRINT 1 And then use it like #if(DEBUG_PRINT == 1) ...
gbmhunter's user avatar
  • 1,801
39 votes
4 answers
24k views

Equivalents to MSVC's _countof in other compilers?

Are there any builtin equivalents to _countof provided by other compilers, in particular GCC and Clang? Are there any non-macro forms?
Matt Joiner's user avatar
39 votes
3 answers
13k views

When do we need #ifdef before #undef?

In many of the C and C++ files I have seen macros like this: #ifdef X #undef X // no statements in between #endif I feel that, it's adequate to simply write: #undef X If the macro X wasn't ...
iammilind's user avatar
  • 69k
38 votes
8 answers
87k views

Debug Print Macro in C?

in C, what is the proper way to define a printf like macro that will print only when DEBUG symbol is defined? #ifdef DEBUG #define DEBUG_PRINT(???) ??? #else #define DEBUG_PRINT(???) ??? #endif ...
John's user avatar
  • 381
38 votes
6 answers
32k views

Visual Studio macro to find a string and delete matching lines

In my Visual Studio (2010 C#) solution, I need to delete all lines of code that contain a matching string pattern. For example, I want to delete all lines that contain ".BackColor = System.Drawing....
Hadster's user avatar
  • 465
38 votes
2 answers
10k views

What's the easiest way to use reify (get an AST of) an expression in Scala?

I'm looking at alternatives to -print or javap as a way of figuring out what the compiler is doing in Scala. With the new reflection/macros library, reify seems a good candidate for that, as shown in ...
Daniel C. Sobral's user avatar
38 votes
5 answers
18k views

Can I comment multi-line macros?

I'm writing a few large macros and thought it'd be nice if I could add comments to them, like this: #define SOME_BIG_MACRO(input)\ SOME_FUNCTION_CALL() \ // this does... SOME_OTHER_FUNCTION_CALL()...
quant's user avatar
  • 22.1k
38 votes
2 answers
10k views

Detecting Integer Constant Expressions in Macros

There was a discussion in the Linux kernel mailing list regarding a macro that tests whether its argument is an integer constant expression and is an integer constant expression itself. One ...
Acorn's user avatar
  • 25.6k
38 votes
3 answers
11k views

What is the difference between a preprocessor macro with no arguments, and one with zero arguments

Is there any reason to prefer #define MY_MACRO() ..stuff.. to #define MY_MACRO ..stuff.. Don't use macros is not a valid answer...
StephQ's user avatar
  • 2,082
37 votes
8 answers
9k views

What can you do with Lisp macros that you can't do with first-class functions?

I think I understand Lisp macros and their role in the compilation phase. But in Python, you can pass a function into another function def f(filename, g): try: ...
Joshua Fox's user avatar
  • 19.1k
36 votes
5 answers
54k views

Implicit declaration of function - C99

I am currently using Xcode 4, and in my .pch file I have this macro: #define localize(s) NSLocalizedString((s), nil). When I try to use this macro in some .m file, I receive this warning: Implicit ...
Misa's user avatar
  • 889
36 votes
1 answer
15k views

How and When to Use @async and @sync in Julia

I have read the documentation for the @async and @sync macros but still cannot figure out how and when to use them, nor can I find many resources or examples for them elsewhere on the internet. My ...
Michael Ohlrogge's user avatar
36 votes
8 answers
12k views

How to save a Vim macro that contains "Escape" key presses?

I use the following Vim macro a lot (it puts the current line inside XML tags): I<e>^[A</e> So I saved it into my .vimrc let @e='I<e>^[A</e>' But it does not work. The ^[ ...
Nicolas Raoul's user avatar
36 votes
2 answers
94k views

Can javascript simulate a button click?

I am designing a site where it would be problematic if macros were allowed to run freely. I have thought of a way to stop a macro made by simulating the HTTP requests from a button click but this ...
AndrewB's user avatar
  • 796
36 votes
4 answers
31k views

What do __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS mean?

I see this in the standard C++ libraries for my system, as well as some of the headers in a library I'm using. What are the semantics of these two definitions? Is there a good reference for #defines ...
Matthew Willis's user avatar
36 votes
13 answers
16k views

What useful macros have you created in Netbeans? [closed]

I use Netbeans (nightly build) for Ruby on Rails development and I'm looking to beef up my macros. I've created a few myself: copy identifier: select-identifier copy-to-clipboard paste clipboard ...
Richard Poirier's user avatar
36 votes
2 answers
4k views

Scala macros and the JVM's method size limit

I'm replacing some code generation components in a Java program with Scala macros, and am running into the Java Virtual Machine's limit on the size of the generated byte code for individual methods (...
Travis Brown's user avatar
35 votes
9 answers
27k views

How do I implement no-op macro (or template) in C++?

How do I implement no-op macro in C++? #include <iostream> #ifdef NOOP #define conditional_noop(x) what goes here? #else #define conditional_noop(x) std::cout <&...
Anton Daneyko's user avatar
35 votes
8 answers
17k views

how to find the source of some macros

There are many places for defining a macro.When the macro is defined in our own project by us,the are easy to find the definition position for them. But when i try to learn some famous open source ...
basketballnewbie's user avatar
35 votes
8 answers
4k views

Examples of what Lisp's macros can be used for

I've heard that Lisp's macro system is very powerful. However, I find it difficult to find some practical examples of what they can be used for; things that would be difficult to achieve without them. ...
Dimitri C.'s user avatar
  • 22.2k
35 votes
2 answers
25k views

What is the __STDC_VERSION__ value for C11?

I know that compilers use __STDC__ to indicate that a compiler is standard C and, from, there, you can use __STDC_VERSION__ to figure out which level of the standard you're using. I also know that ...
paxdiablo's user avatar
  • 868k
34 votes
3 answers
28k views

Does Q_UNUSED have any side effects?

Given the following piece of code: void test(int var) { Q_UNUSED(var); #ifdef SOMETHING printf("%d",var); //do something else with var... #endif } Would the Q_UNUSED macro have any ...
Ilya Kobelevskiy's user avatar
34 votes
1 answer
3k views

Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc

I'm bewildered by all the built-in Mathematica functions that purport to prevent evaluation in some way: Unevaluated, Defer, Hold, and over half a dozen of the form Hold*. The Mathematica ...
dreeves's user avatar
  • 26.7k
33 votes
6 answers
55k views

Implement generic swap macro in C [duplicate]

Possible Duplicate: is there an equivalent of std::swap() in c Hi folks, I was attempting a problem to write a generic swap macro in C and my macro looks like this: #define swap(x,y) { x = x + ...
Mohit Dhuper's user avatar
33 votes
7 answers
30k views

Macros to create strings in C

Alternative titles (to aid search) Convert a preprocessor token to a string How can I make a char string from a C macro's value? Original Question I would like to use C #define to build literal ...
Richard Stelling's user avatar
33 votes
7 answers
21k views

C++ - enum vs. const vs. #define

At the end of the article here: http://www.learncpp.com/cpp-tutorial/45-enumerated-types/, it mentions the following: Finally, as with constant variables, enumerated types show up in the debugger, ...
Simplicity's user avatar
  • 48.2k
33 votes
3 answers
21k views

How do I match the type of an expression in a Rust macro?

This is just pseudocode: macro_rules! attribute { $e: expr<f32> => { /* magical float stuff */ }; $e: expr<i64> => { /* mystical int stuff */ }; }; I would like to have a ...
Arne's user avatar
  • 8,177
33 votes
4 answers
11k views

Is it possible to declare variables procedurally using Rust macros?

Basically, there are two parts to this question: Can you pass an unknown identifier to a macro in Rust? Can you combine strings to generate new variable names in a Rust macro? For example, something ...
Doug's user avatar
  • 34k
32 votes
18 answers
19k views

The most useful user-made C-macros (in GCC, also C99)? [closed]

What C macro is in your opinion is the most useful? I have found the following one, which I use to do vector arithmetic in C: #define v3_op_v3(x, op, y, z) {z[0]=x[0] op y[0]; \ ...
psihodelia's user avatar
  • 29.9k
32 votes
7 answers
22k views

How to repeat some action certain times on Vim?

In Vim, I usually want to repeat some series of commands some times. Say, I want to comment 5 lines, I would use I//<Esc>j .j.j.j.j Is there any way to repeat the last ".j" part several times?
Martín Fixman's user avatar