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
61
votes
2 answers

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: Passing a list to a CMake macro When am I supposed to add…
Vince
  • 3,979
  • 10
  • 41
  • 69
60
votes
8 answers

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: Andrew Wright and Bruce Duba. Pattern matching for…
unj2
  • 52,135
  • 87
  • 247
  • 375
58
votes
3 answers

How do I check for C++20 support? What is the value of __cplusplus for C++20?

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, in principle, possible to inquire the C++ version…
user2296653
  • 1,151
  • 1
  • 8
  • 17
58
votes
5 answers

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 compiler? We are using here G++ 4.4.x, Visual Studio…
math
  • 8,514
  • 10
  • 53
  • 61
58
votes
6 answers

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. However, sometimes I find that testing __OSX__ only is…
mloskot
  • 37,086
  • 11
  • 109
  • 136
58
votes
5 answers

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 used at runtime, therefor it is unavailable, but…
Morgan
  • 1,765
  • 2
  • 19
  • 26
57
votes
4 answers

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 give me a hint? Thanks!
Anders Lind
  • 4,542
  • 8
  • 46
  • 59
56
votes
4 answers

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 bad? P.S.: Surprisingly I couldn't find any detailed…
AnkithD
  • 743
  • 6
  • 9
56
votes
6 answers

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 -
jww
  • 97,681
  • 90
  • 411
  • 885
55
votes
9 answers

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 like #define READINT Scanner(System.in).nextInt();…
Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77
55
votes
7 answers

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 DATE_as_int_str useD(__DATE__) // What can be done…
Rick2047
  • 1,565
  • 7
  • 24
  • 35
54
votes
2 answers

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 an error message, but I can see valid C code by…
Jack
  • 16,276
  • 55
  • 159
  • 284
53
votes
11 answers

Why use Macros in C?

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 understanding of macros is they're a simple search…
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
52
votes
3 answers

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 is not a method of Foo. I can manually type: from(input:…
bright-star
  • 6,016
  • 6
  • 42
  • 81
52
votes
11 answers

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 lacking compared to what I grew to love in C/C++.…
Andrew Garrison
  • 6,977
  • 11
  • 50
  • 77