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
13,707
questions
77
votes
5
answers
108k
views
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 #...
73
votes
5
answers
11k
views
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":
...
72
votes
1
answer
2k
views
Documenting Scala 2.10 macros [closed]
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 {
...
71
votes
2
answers
19k
views
*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 ...
69
votes
25
answers
30k
views
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 ...
68
votes
2
answers
22k
views
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 ...
66
votes
6
answers
59k
views
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 arguments to GCC, like -D, but I can't get to see the "not defined" message.
#include <iostream>
#...
65
votes
4
answers
3k
views
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 ...
65
votes
3
answers
2k
views
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) "" ...
65
votes
7
answers
46k
views
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 ...
62
votes
2
answers
31k
views
When should I quote CMake variable references?
I am writing CMake macros for the first time, and I have a hard time understanding how variables work. Most specifically, ${a} seems to have a different meaning than "${a}".
For example here:...
62
votes
3
answers
27k
views
How do I check for C++20 support? What is the value of __cplusplus for C++20? [duplicate]
Related to questions How do I check for C++11 support? and What is the value of __cplusplus for C++17?
How can I inquire whether the compiler can handle / is set up to use C++20? I know that it is, ...
62
votes
5
answers
91k
views
Which Cross Platform Preprocessor Defines? (__WIN32__ or __WIN32 or WIN32 )?
I often see __WIN32, WIN32 or __WIN32__. I assume that this depends on the used preprocessor (either one from visual studio, or gcc etc).
Do I now have to check first for os and then for the used ...
61
votes
6
answers
42k
views
Which macro to wrap Mac OS X specific code in C/C++
While reading various C and C++ sources, I have encountered two macros __APPLE__ and __OSX__. I found plenty of use of __OSX__ in various codes, especially those originating from *BSD systems.
...
60
votes
8
answers
3k
views
Collection of Great Applications and Programs using Macros
I am very very interested in Macros and just beginning to understand its true power. Please help me collect some great usage of macro systems.
So far I have these constructs:
Pattern Matching:
...
59
votes
3
answers
43k
views
Can I convert a string to enum without macros in Rust?
For example, if I have code like:
enum Foo {
Bar,
Baz,
Bat,
Quux
}
impl Foo {
from(input: &str) -> Foo {
Foo::input
}
}
This will obviously fail because input ...
59
votes
5
answers
84k
views
Retrieving a c++ class name programmatically
I was wondering if it is possible in C++ to retrieve the name of a class in string form without having to hardcode it into a variable or a getter. I'm aware that none of that information is actually ...
59
votes
6
answers
49k
views
What is the value of __cplusplus for C++17?
We are trying to test some code under C++17 and its change to std::uncaught_exception. I can't seem to get GCC to provide the value of __cplusplus:
$ /opt/local/bin/g++ -std=c++17 -dM -E - </dev/...
59
votes
4
answers
33k
views
What does #x inside a C macro mean?
For example I have a macro:
#define PRINT(int) printf(#int "%d\n",int)
I kinda know what is the result.
But how come #int repersent the whole thing?
I kinda forget this detail. Can anybody kindely ...
57
votes
9
answers
88k
views
Can I have macros in Java source files
In my program I'm reading integers form console many times. Every time, I need to type this line.
new Scanner(System.in).nextInt();
I'm used to C/C++ and I'm wondering if I can define something ...
56
votes
4
answers
10k
views
What is double evaluation and why should it be avoided?
I was reading that in C++ using macros like
#define max(a,b) (a > b ? a : b)
can result in a 'double evaluation'. Can someone give me an example of when a double evaluation occurs and why it's ...
55
votes
11
answers
69k
views
Why use Macros in C? [duplicate]
Possible Duplicate:
What are C macros useful for?
Every few months I get an itch to go learn some bit of C that my crap college programming education never covered. Today it's macros. My basic ...
55
votes
7
answers
148k
views
How to use __DATE__ and __TIME__ predefined macros in as two integers, then stringify?
Want to use __ DATE __ and __ TIME __ as integer for giving automated version to my code in compile time.
#define STRINGIZER(arg) #arg
#define STR_VALUE(arg) STRINGIZER(arg)
#define ...
54
votes
4
answers
18k
views
Is there a way to display a macro list similar to displaying your mappings in Vim?
I know there is a way to list mappings via :map (or :imap, :cmap, etc.), but I can't find a way to list macros I have stored in my vimrc file (as in let @a = 'blahblah').
Is there a way to do this ...
54
votes
2
answers
52k
views
error: pasting "." and "red" does not give a valid preprocessing token
I'm implementing The X macro, but I have a problem with a simple macro expansion. This macro (see below) is used into several macros usage examples, by including in this article.
The compiler gives ...
53
votes
0
answers
1k
views
Materialize the Value for a Type that has One Inhabitant
Thanks to @MilesSabin's answer I can write a type level Fibonacci sequence:
sealed trait Digit
case object Zero extends Digit
case object One extends Digit
sealed trait Dense { type N <: Dense }
...
52
votes
11
answers
31k
views
Why aren't there macros in C#?
When learning C# for the first time, I was astonished that they had no support for macros in the same capacity that exists in C/C++. I realize that the #define keyword exists in C#, but it is greatly ...
52
votes
4
answers
68k
views
Is there a way to get function name inside a C++ function?
I want to implement a function tracer, which would trace how much time a function is taking to execute. I have following class for the same:-
class FuncTracer
{
public:
FuncTracer(LPCTSTR ...
52
votes
1
answer
15k
views
WRITE_ONCE in linux kernel lists
I am reading the linux kernel implementation of doubled linked list. I do not understand the use of the macro WRITE_ONCE(x, val). It is defined as follow in compiler.h:
#define WRITE_ONCE(x, val) x=(...
51
votes
8
answers
48k
views
How to generate a newline in a cpp macro?
How do I write a cpp macro which expands to include newlines?
50
votes
14
answers
8k
views
How far can LISP macros go? [closed]
I have read a lot that LISP can redefine syntax on the fly, presumably with macros. I am curious how far does this actually go? Can you redefine the language structure so much that it borderline ...
50
votes
5
answers
8k
views
Lazy Evaluation vs Macros
I'm used to lazy evaluation from Haskell, and find myself getting irritated with eager-by-default languages now that I've used lazy evaluation properly. This is actually quite damaging, as the other ...
49
votes
6
answers
16k
views
Equivalent of __func__ or __FUNCTION__ in Rust?
In C and C++ you can get the name of the currently executing function through the __func__ macro with C99 & C++11 and ___FUNCTION___ for MSVC.
Is there an equivalent of this in Rust?
Example of ...
49
votes
7
answers
25k
views
Variadic recursive preprocessor macros - is it possible?
I've run into a little theoretical problem. In a piece of code I'm maintaining there's a set of macros like
#define MAX_OF_2(a, b) (a) > (b) ? (a) : (b)
#define MAX_OF_3(a, b, c) MAX_OF_2(...
49
votes
3
answers
12k
views
Opposite of C preprocessor "stringification"
When using C preprocessor one can stringify macro argument like this:
#define TO_STRING(x) "a string with " #x
and so when used, the result is as follows:
TO_STRING(test) will expand to: "a string ...
48
votes
4
answers
46k
views
Empty function macros
If I define a function macro with no actual body, is it like an empty string with the compiler (i.e. It doesn't generate any extra instructions at compile time)?
Example:
#define SomeMacro(a)
...
48
votes
5
answers
143k
views
Error: macro names must be identifiers using #ifdef 0
I have the source code of an application written in C++ and I just want to comment something using:
#ifdef 0
...
#endif
And I get this error
error: macro names must be identifiers
Why is this ...
47
votes
8
answers
45k
views
Escaping a # symbol in a #define macro?
Without going into the gory details I want to use a #define macro that will expand to a #include but the '#' sign is confusing the preprocessor (as it thinks I want to quote an argument.)
For example,...
47
votes
3
answers
15k
views
Can I redefine a C++ macro then define it back?
I am using both the JUCE Library and a number of Boost headers in my code. Juce defines "T" as a macro (groan), and Boost often uses "T" in it's template definitions. The result is that if you ...
47
votes
1
answer
13k
views
Generating documentation in macros
I have a couple of macros to reduce boilerplate when defining certain tuple-structs of the form:
macro_rules! new_type (($name:ident, $bytes:expr) => (
pub struct $name(pub [u8; $bytes]);
/...
47
votes
5
answers
58k
views
Portable UNUSED parameter macro used on function signature for C and C++
I'm interested in creating a macro for eliminating the unused variable warning.
This question describes a way to suppress the unused parameter warning by writing a macro inside the function code:
...
46
votes
21
answers
14k
views
C/C++: How to use the do-while(0); construct without compiler warnings like C4127?
I'm often using do-while(0) constructs in my #defines, for the reasons described in this answer. Also, I'm trying to set the compiler's warning level as high as possible to catch more potential ...
46
votes
7
answers
36k
views
What makes a better constant in C, a macro or an enum?
I am confused about when to use macros or enums. Both can be used as constants, but what is the difference between them and what is the advantage of either one? Is it somehow related to compiler level ...
46
votes
3
answers
9k
views
Run a macro in all buffers in vim
I know about the :bufdo command, and was trying to combine it with a macro I had recorded (@a) to add a #include in the proper spot of each of the header files I'd loaded. However, I couldn't find an ...
44
votes
3
answers
2k
views
So many parentheses in gcc standard headers
Why are constant expressions in GCC header files surrounded by parentheses, like this?
#define INTMAX_MIN (-9223372036854775807LL)
#define INTMAX_MAX (9223372036854775807LL)
What would be the ...
44
votes
4
answers
6k
views
threading macro -> with anonymous functions
I understand the `-> theading macro in Clojure applies all the provided functions provided to a given argument. However, it doesn't seem to work with anonymous functions. For example:
user> (-> ...
44
votes
2
answers
57k
views
What's the difference between #if and #ifdef Objective-C preprocessor macro?
How to define preprocessor macros in build settings, like IPAD_BUILD, and IPHONE_BUILD (and how to use them in my factory methods)?
I'm using these by heart now, would be cool to know what is going ...
43
votes
5
answers
5k
views
How to call std::min() when min has been defined as a macro?
How do I call std::min when min has already been defined as a macro?
43
votes
7
answers
22k
views
do while(false) pattern [duplicate]
Possible Duplicate:
Why use apparently meaningless do-while and if-else statements in macros?
Why is the do while(false) necessary in the macros below?
#define LOG(message, ...) \
do { \
Lock<...
43
votes
4
answers
15k
views
Running a macro till the end of text file in Emacs
I have a text file with some sample content as shown here:
Sno = 1p
Sno = 2p
Sno = 3p
What i want is to remove the p from each of the columns.
With this intention i write a macro:
M-x //go to ...