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
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 ...
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) ...
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 ...
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)
...
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 ...
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",...
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! ...
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 ...
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: ...
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(...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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)?
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:
...
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? ...
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 ...
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 ...
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.
...
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 ...
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
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();
)
...
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 ...
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 ...
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 ...
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)...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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++?
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 [^...
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 ...
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).
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) ...