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
25 votes
5 answers
13k views

How can you compare two character strings statically at compile time

I would like to create a macro which can compare 2 strings, and emit a compile time error if the condition isn't met. This could be though of as a compile time assertion. I'm not sure how I could do ...
Avba's user avatar
  • 15k
25 votes
2 answers
31k views

C Macro Question -(x) vs (-x)

I'm going through quiz answers from my professor and a question was: the correct implementation of a function like macro for absolute value is: #define abs(x) ((x)<0 ? (-x) : (x)) #define abs(x) ...
Crystal's user avatar
  • 28.9k
25 votes
2 answers
4k views

multiple arity in defmacro of clojure

I encountered a strange problem relating to defmacro in Clojure, I have code like (defmacro ttt ([] (ttt 1)) ([a] (ttt a 2)) ([a b] (ttt a b 3)) ([a b c] `(println ~a ~b ~c))) and I run with ...
xudifsd's user avatar
  • 800
25 votes
2 answers
25k views

Too many arguments provided to function-like macro invocation [duplicate]

Say we have an implementation of std::aligned_storage. I've defined two macros for the alignof and alignas operators. #include <iostream> #include <cstddef> #define ALIGNOF(x) alignof(x) ...
DeiDei's user avatar
  • 10.3k
25 votes
2 answers
3k views

Clojure defmacro loses metadata

I am trying to create a little Clojure macro that defs a String with a type hint: (defmacro def-string [name value] `(def ^String ~name ~value)) (def-string db-host-option "db-host") When I ...
Ralph's user avatar
  • 32k
25 votes
1 answer
2k views

C strange macro syntax

I found this C code example, and I am absolutely puzzled: #include <stdio.h> #define M(a,b) a%:%:b main() { int a=1, b=2, ab[]={10,20}, c; printf( "%d", M(a,b)<:a:> ); printf( "%d",...
Eutherpy's user avatar
  • 4,541
25 votes
1 answer
27k views

How do I create a Rust macro with optional parameters using repetitions?

I'm currently looking into Rust macros and I can not find any detailed documentation on repetitions. I would like to create macros with optional parameters. This would be my idea: macro_rules! ...
Stein's user avatar
  • 3,209
25 votes
7 answers
15k views

Is it OK to use a code block as an argument for a C macro?

I have a pattern that is basically some boilerplate code with a part that varies in the middle if(condition){ struct Foo m = start_stuff(); { m.foo = bar(1,2); m.baz = 17; } //this part ...
hugomg's user avatar
  • 69.1k
25 votes
1 answer
1k views

What is the reason for having unreserved identifiers as built-in macros in gcc?

Today I stumbled upon a rather interesting compiler error: int main() { int const unix = 0; // error-line return unix; } Gives the following message with gcc 4.3.2 (yes, ancient...): error: ...
Matthieu M.'s user avatar
24 votes
3 answers
4k views

C++ macro '##' doesn't work after '->' operator

I have a shared_ptr object x, which has get and set methods as follows: x->a_value(); x->set_a_value(); x->b_value(); x->set_b_value(); When i try to define a macro: #define MAC(...
250's user avatar
  • 659
24 votes
9 answers
29k views

Best AutoHotKey macros? [closed]

I use AutoHotKey for Windows macros. Most commonly I use it to define hotkeys that start/focus particular apps, and one to send an instant email message into my ToDo list. I also have an emergency ...
leeborkman's user avatar
24 votes
6 answers
13k views

Vim - Find pattern on currently line ONLY

I'm wondering if there is a way to find a pattern, but restrict it to the current line. Basically, the equivalent of /PATTERN but restricted to the current line, rather than the entire document. I've ...
Austin's user avatar
  • 437
24 votes
2 answers
31k views

how to implement macros in Go?

I've project done in C++ where I used #define macros to give a name of the project, which I used in several places, I don't change this name often but sometimes I may need to change this, then I ...
Pavani siva dath's user avatar
24 votes
5 answers
5k views

Generating an error if checked boolean macro is not defined

I have several configuration files each one containing the definition of some boolean macro, to be set to 0 or 1. Then, in my code, I check the value of such a macro to decide which part of the code ...
Antonio's user avatar
  • 19.8k
24 votes
1 answer
5k views

Linux Kernel's __is_constexpr Macro

How does the __is_constexpr(x) macro of the Linux Kernel work? What is its purpose? When was it introduced? Why was it introduced? /* * This returns a constant expression while determining if an ...
Acorn's user avatar
  • 25.6k
24 votes
3 answers
3k views

Racket vs Scheme macros

Do racket macros have more advanced functionality than those found in Scheme or Common Lisp? I suspect so, especially regarding modules, namespaces and scoping, etc, but I'd appreciate a simple ...
Scott Klarenbach's user avatar
24 votes
2 answers
13k views

Difference between #pragma and _Pragma() in C

What is the difference between #pragma and _Pragma() in C? syntax: #pragma arg and _Pragma(arg) When should I use _Pragma(arg)?
Jayesh's user avatar
  • 4,863
24 votes
2 answers
17k views

How do I show the value of a #define at compile time in gcc

So far I've got as far as: #define ADEFINE "23" #pragma message ("ADEFINE" ADEFINE) Which works, but what if ADEFINE isn't a string? #define ADEFINE 23 #pragma message ("ADEFINE" ADEFINE) causes: ...
John Lawrence Aspden's user avatar
24 votes
2 answers
8k views

C macros and use of arguments in parentheses

Example #define Echo(a) a #define Echo(a) (a) I realize there probably isn’t a significant difference here, but why would you ever want to include the a within parenthesis inside the macro body? ...
rubixibuc's user avatar
  • 7,209
24 votes
4 answers
27k views

MSBuild.exe not accepting either /p:DefineConstants nor /p:PreprocessorDefinitions

I've been through quite a number of articles on Stack Overflow that answered the question "How do I pass preprocessor definitions to the compiler from the MSBuild command line," and they all responded ...
Sniggerfardimungus's user avatar
24 votes
1 answer
27k views

What is EXPORT_SYMBOL_GPL in Linux kernel code?

What is EXPORT_SYMBOL_GPL in Linux kernel code? Below is a piece of code, which contains EXPORT_SYMBOL_GPL 62 struct resource *platform_get_resource(struct platform_device *dev, 63 ...
Sagar Jain's user avatar
  • 7,653
24 votes
1 answer
30k views

Recording a 'macro'? or a series of actions in Visual Studio Code?

I would need something like this. Once I create a folder within a project. A React Project for example. I could select that folder and run some kind of macro or series of actions that would do this. ...
Talmacel Marian Silviu's user avatar
23 votes
7 answers
9k views

Why are C macros not type-safe?

If have encountered this claim multiple times and can't figure out what it is supposed to mean. Since the resulting code is compiled using a regular C compiler it will end up being type checked just ...
Sarien's user avatar
  • 6,802
23 votes
2 answers
9k views

Is there a #define for C99?

I want to do something in C99 one way, otherwise to perform it another way. What is the #define to check for? #ifdef C99 ... #else ... #endif
klynch's user avatar
  • 1,076
23 votes
6 answers
28k views

How can I generate unique values in the C preprocessor?

I'm writing a bunch of related preprocessor macros, one of which generates labels which the other one jumps to. I use them in this fashion: MAKE_FUNNY_JUMPING_LOOP( MAKE_LABEL(); MAKE_LABEL(); ) ...
Andres Jaan Tack's user avatar
23 votes
3 answers
25k views

What predefined macro can I use to detect the target architecture in Clang?

I would like to write code depending on whether the target architecture is e.g. armv7, armv7s, or arm64. The reason that I can't use sysctlbyname is that this would give me the underlying ...
oleks's user avatar
  • 828
23 votes
3 answers
6k views

Is it possible to implement Common Lisp's macro system in scheme?

Hopefully this is not a redundant question. As a newcomer to scheme I am aware that syntax-case macros are more powerful than the syntax-rules alternative, at the cost of unwanted complexity. Is ...
category's user avatar
  • 2,125
23 votes
4 answers
18k views

Is there a way to get the field names of a struct in a macro?

Consider the following example: struct S { a: String, b: String, } I have a macro which is called like this: my_macro!(S); I want to access the field names of the struct in the macro like ...
Joel Hermanns's user avatar
23 votes
2 answers
21k views

How do I define preprocessor macros in Xcode 4?

I have two targets set up for my app (a lite version and a pro version) and I want to integrate some subtle differences in the code for each of them (e.g. the pro version will not show any iAd banners)...
user avatar
23 votes
4 answers
21k views

How can I access template variable in TWIG macro?

I'm not able to access template variable in TWIG macro. Here is a simplified example: {% set myname = "Ligio" %} {{ _self.pagedurl(1) }} {% macro pagedurl(page) %} Hi {{ _self.myname }}! This ...
Ligio's user avatar
  • 617
23 votes
2 answers
7k views

How to tame the Windows headers (useful defines)?

In one of the answers to this question jalf spoke about useful define NOMINMAX, that could prevent from unwanted defining min/max macros. Are there other useful defines that can help to control ...
Kirill V. Lyadvinsky's user avatar
23 votes
8 answers
29k views

Compile-time string encryption

I don't want reverse-engineers to read the plain-text of hardcoded strings in my application. The trivial solution for this is using a simple XOR-Encryption. The problem is I need a converter and in ...
Listing's user avatar
  • 1,181
23 votes
4 answers
4k views

How do you compile macros in a Lisp compiler?

In a Lisp interpreter, there can easily be a branch in eval that can expand a macro, and in the process of expanding it, call functions to build up the expanded expression. I've done this before using ...
apg's user avatar
  • 2,641
23 votes
1 answer
5k views

Understanding DEFER and OBSTRUCT macros

I created a small macro metaprogramming library that implements basic useful constructs such as REPEAT(times, x), IF(value, true, false), tuples, and more. Most of my implementations work by ...
Vittorio Romeo's user avatar
23 votes
5 answers
12k views

What's the Swift equivalent of Objective-C's "#ifdef __IPHONE_11_0"?

I want to use Xcode 9 to add iOS 11 code to my project while keeping the option to compile the project with Xcode 8 which only supports iOS 10. In Objective-C I can do this by using a preprocessor ...
Matthias Bauch's user avatar
23 votes
4 answers
15k views

What is the type of the __LINE__ macro in C++

What is the type of the __LINE__ macro in C++?
prabhakaran's user avatar
  • 5,208
23 votes
9 answers
19k views

How to debug macros efficiently in VS?

I've got a pretty complicated macro inside my (unmanaged) C++ code. Is there any way to expand macros in VS debugger? Or maybe there is another way to debug macros there? F.e. I'd like to place a ...
liori's user avatar
  • 41.5k
23 votes
3 answers
9k views

Generate include file name in a macro

I'm trying to generate include file name in macro. This is supposed to be legal in C++: #define INCLUDE_FILE "module_impl_win.hpp" #include INCLUDE_FILE this works fine, but as soon as I try to ...
ledokol's user avatar
  • 461
23 votes
2 answers
48k views

What does ##__VA_ARGS__ mean?

I would like to know what ## does in this macro definition: #define debug(M, ...) fprintf(stderr,M "\n",##__VA_ARGS __) I googled for an answer and I came up with the following. The ## will remove ...
Mohammed Micro's user avatar
22 votes
10 answers
50k views

How to delete all comments in a selected code section?

Every once in a while, my code gets littered with many useless comments, most of them are obsolete lines of code, and some are obsolete "memos to self". So I was wondering if there's a way to just ...
Oren A's user avatar
  • 5,890
22 votes
5 answers
14k views

How can I use the format! macro in a no_std environment?

How could I implement the following example without using std? let text = format!("example {:.1} test {:x} words {}", num1, num2, num3); text has type &str and num1, num2 and num3 have any ...
Ferdia McKeogh's user avatar
22 votes
4 answers
10k views

How to remove the enclosing parentheses with macro?

No comma is allowed in a macro argument because it will be treated as more than one arguments and the preprocessing will be wrong. However, we can parenthesize the argument to let preprocessor treat ...
user1899020's user avatar
  • 13.4k
22 votes
5 answers
65k views

How to prevent macro redefinition

After working some time on my project, this warning begins to appear: 2>Game.cpp 2>c:\program files\microsoft sdks\windows\v6.0a\include\windef.h(126) : warning C4005: 'APIENTRY' : redefinición ...
Killrazor's user avatar
  • 7,006
22 votes
3 answers
23k views

How to import macros in Rust?

I'm struggling with how to import macros from an external crate. In my main.rs I'm importing the Glium crate: #![macro_use] extern crate glium; pub use glium::*; // where my actual main function ...
Adityo Pratomo's user avatar
22 votes
1 answer
13k views

How do I stop running Vim macro

I recorded a macro to a register and started it with too many repetitions. It takes way to much time to complete each macro. How do I cancel/stop Vim executing macro? Is there a way by doing it ...
BlueNile's user avatar
  • 401
22 votes
8 answers
9k views

How do I avoid name collision with macros defined in Windows header files?

I have some C++ code that includes a method called CreateDirectory(). Previously the code only used STL and Boost, but I recently had to include <windows.h> so I could look-up ...
Mike Willekes's user avatar
22 votes
1 answer
2k views

Generating Clojure code with macro containing type hints

I'm trying to generate some Clojure code with type hints, however the type hints seem to disappear whenever I build some code (they also don't function when the code is compiled) e.g. `(let [^...
mikera's user avatar
  • 106k
22 votes
2 answers
5k views

How do I define a macro with multiple pragmas for Clang?

I'd like to add some macros to ease (un)setting a specific warning around routines that we are deprecating internally. I'd like to turn this: #pragma clang diagnostic push #pragma clang diagnostic ...
fbrereto's user avatar
  • 35.8k
22 votes
4 answers
12k views

List of the $(xxx) macro in visual studio

I would like to establish a list of all the visual studio macro that can be used in the post-build event script. $(Configuration) : Name of the current build configuration (ie: Debug or Release).
Martin Delille's user avatar
22 votes
2 answers
3k views

Incompatible pointer types passing in _Generic macro

The following code generates 2 warnings which are described in the question's title. #include <stdio.h> static void _print_f(float *f){printf("float : %f\n", *f);} static void _print_i(int *i) ...
Peter Varo's user avatar
  • 11.9k

1
3 4
5
6 7
275