13,864 questions
Best practices
0
votes
5
replies
84
views
Should I pass as an argument a value that a macro can infer from the code?
Let's take a silly example:
#define MAX_NUM_BITES 20
...
void live_another_day() {
...
int num_bites = 0;
...
eat_little_pig(&num_bites);
if (num_bites>MAX_NUM_BITES) {
printf(&...
2
votes
1
answer
106
views
Using macros with dynamic name using dot operator does not work
While upgrading twig v1.30 to v3.22.0 i encountered some problem using macros from an external file and calling them by using a variable value as dynamic name.
Given the twig file with the macros:
{# ...
Advice
0
votes
7
replies
135
views
C Preprocessor: foreach/macro expansion with loop context
So I've been using this code for generating such statements following This Reddit post:
#define LOOP(seq) END(A seq)
#define BODY(x) int x;
#define A(x) BODY(x) B
#define B(x) BODY(x) A
#define A_END
#...
5
votes
1
answer
166
views
Why does the same code, one generated by macros and the other handwritten, produces different results in MSVC?
The same code, one generated by macros and the other handwritten, produces different results.I'm lost and don't know how to fix it.
my environment is follow:
vs2019, msvc2019_64, c++14
if using ...
2
votes
1
answer
114
views
C macro compilation fails in Clang with "expected ;" due to phantom comma in clang -E expansion
I wrote my "Result" macro header, using C23 and some of the newest features:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define __RESULT_EAT_PARENS(...) __VA_ARGS__
...
2
votes
1
answer
107
views
Macros not working as I expect on Windows
I have the below code I want to print Use skeleton! if SKELETON_ENABLED is defined and is not running on MAC OS, and Don't use skeleton!\n otherwise.
The program prints Don't use skeleton!\n, even ...
1
vote
1
answer
71
views
Scheme: calling `define-syntax-rule` in a loop
I wonder whether it is possible to call define-syntax-rule in a loop-like construction like for-each. I explicitly say 'loop-like' because I know that normal Scheme evaluation comes after macro ...
2
votes
1
answer
109
views
Timer 0 macro constants for Microchip PIC18F8722
I am using MPLAB X IDE version 5.43 to develop an embedded project for a PIC18F8722 microcontroller.
It has a timer TMR0 which can be configured to be 32 bits.
I had lots of statements:
TMR0L = 0;
...
5
votes
2
answers
159
views
Rust impl trait for type if it has trait
I want to create a macro that impements a trait A for a type if it implements a trait B.
The macro I implement will be given a list of types (String, i32, ...).
Then for each argument I want to ...
0
votes
0
answers
44
views
TexStudio macro running python script but writing return string in message box
I make my drawings in Krita while I write my report in TexStudio. In Krita I select part of the drawing and put it on the clipboard. I put my cursor in the .tex file at the desired position and run ...
1
vote
1
answer
66
views
Bypassing A2041 in a VARARG macro
I have a pretty simple ENUM macro in MASM32 that uses VARARGs to assign incrementing values to symbols:
ENUM MACRO vargs:VARARG
LOCAL num_
num_=0
FOR varg,<vargs>
varg EQU ...
2
votes
3
answers
239
views
How to detect C++20 mode of compilation?
When compiling my program using a recent version of OpenVDB library by Clang in C++20 mode, I get the error:
error: use of the 'assume' attribute is a C++23 extension
I see that it is due to the ...
0
votes
1
answer
104
views
Rust macro with #[no_mangle] function triggers 'unsafe attribute' error when applied to a struct
I'm trying to implement a Rust procedural macro that exports a C-compatible function for a driver I'm writing. Here's my macro:
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
...
0
votes
1
answer
57
views
How to include a file with shadow-cljs/inline, but preprocess it first?
Shadow-cljs has a resource loader that can include literal files into the Clojurescript code
(ns app.my
(:require
[shadow.resource :as rc]))
(rc/inline "file.txt")
I need a macro that ...
2
votes
1
answer
147
views
Using C++ macros to construct a method name to call
I have a class Foo from a third party library that has a lots of setters with a string argument named as set_parameter_name(std::string value). And I need to configure an instance of the class using a ...
3
votes
2
answers
190
views
C macro to compute array length that returns 0 for NULL
I am writing hardcoded calls of functions that takes array(s) in parameter, e.g.
int foo(size_t length, const int array[/* length */]);
In order to avoid the error-prone process of keeping track of ...
0
votes
0
answers
157
views
Stop Condition for Self-Referential Marcos
Background
I am on an ARM Cortex-A72. To access bit fields of my core's system registers I use a macro code generation mechanism. For the sake of simplicity, I replaced void operator=(const uint64_t v)...
0
votes
3
answers
257
views
How to do macro check for endianness without any temp variable?
I was asked to write a macro that checks whether the system is little-endian or big-endian, without using a variable, and for C89.
C89 means lot of solutions requiring more recent versions of C cannot ...
5
votes
1
answer
127
views
How to create meaningful error messages from _Generic macros?
Lets say I have some manner of type safety macro based on _Generic, which I use to wrap an actual function call to make it type safe:
#include <stdio.h>
void print (int i)
{
printf("%d\n&...
1
vote
0
answers
82
views
Editing fields in LibreOffice Writer with Python marco using scriptforge library
The goal is to write a Python macro that creates a new Writer document from a form letter template and fills in editable fields to automate repetitive correspondence generation. My problem is that I ...
0
votes
1
answer
88
views
macro expand to inner attribute and inner comment
I'm trying to make some stability promises for my library. Ideally I'd like to be able to mark the top of a file as stable/unstable, and emit some things as a result.
Unstable allows unused, and is ...
2
votes
1
answer
122
views
NASM - empty macro with 2 parameters? What register is represented by parameter_m?
I'm in the process of porting NASM code to MASM, and I'm stuck on a NASM empty macro with 2 parameters and register usage with the _m suffix.
example code:
;register defines
%define arg0 rcx
%define ...
-1
votes
2
answers
159
views
What does the macro condition "#ifdef __augmented" check in C?
#ifdef __augmented
#define Prefix "@"
#else
#define Prefix ""
#endif
#ifdef __cplusplus
#define Suffix "++"
#else
#define Suffix "&...
3
votes
1
answer
168
views
Use pointer and call function in C++
There are two classes A and B, each having functions f and g (with the same signatures).
The function bool is_A(uintptr_t ptr) returns true if and only if ptr points to an instance of A, and ...
1
vote
1
answer
59
views
My Printer Macros Print documents on Some Printers and Silently Exit The Job on Others
I have been working on a printer macro that uses Python to paste a letterhead onto a PDF message and then send it to the printer to print. In most of the companies' offices I set it up in, it works ...
0
votes
1
answer
137
views
Check the existence and value of a SAS macro variable in one line
I'd like to use a little macro logic in open code (i.e., not within a macro) to control my program. Specifically, if a certain macro variable exists and has a certain value of interest, I'd like to ...
3
votes
3
answers
159
views
using macros to write #defines in C
I have a need to use a macro to write #defines and other macros in C for an embedded MCU.
With the following definitions:
// Define the pin state as wired to the board, CA (PIN controls -ve lead) ...
1
vote
1
answer
118
views
Modern C++ Alternative to Macros for Wrapping OpenGL/SDL State Functions
I'm developing a C++20 application using OpenGL and SDL3, with a window_manager class that wraps windowing and OpenGL state management. Instead of writing separate member functions for each OpenGL/SDL ...
2
votes
0
answers
94
views
Custom Macros for Writing Markdown in VS Code
TL;DR: I want to write (basic) custom macros that VS Code can render in markdown files
I use VS Code with the Markdown Preview Enchanced extension to write mathematical documents in Markdown. I often ...
3
votes
0
answers
59
views
Can the GNU Assembler perform all macro expansions in a file and then output compilable code without macros?
Is there a way to have the GNU assembly process a source file that contains macros, expand them, and then output the expanded equivalent code in a form that could be assembled by as? Basically, I'm ...
3
votes
1
answer
161
views
__FILE__ without full path
The C++ macro __FILE__ typically includes the full path (with many compilers).
For my tracing, I want just the file name, not the full path.
Is there a built-in macro for this in any version of C++ or ...
1
vote
1
answer
65
views
How to write a template or generic type for similar data types with different size of type?
I wrote couple data types (struct) to store small arrays and copy them by values:
Array4 { base: int16|short, purpose: to store up to 4 values }
Array8 { base: int32|int, purpose: to store up to 8 ...
1
vote
1
answer
88
views
LISP macro indentation for sub-expressions?
I'm using Emacs, and I have a macro that's like a switch for regexes, and runs an expression with variables bound to match results.
The indentation isn't what I'd like but I can't find if there's a ...
0
votes
1
answer
87
views
Replacement for `x.asType match { case '[type t <: S; t] => ...` in Scala 3.3 (LTS)
Do we have any replacement for the following Scala 3.7 code:
x.asType match {
case '[type t <: S; t] =>
'{ apply[t] }.asExprOf[Any]
case _ =>
report.errorAndAbort(s"...
0
votes
3
answers
95
views
Extracting the doxygen comments of a C++ method using a macro
I'm trying to find an example of writing a C++ macro that can extract the entire doxygen string, or specific parts of a method for example:
/**
* Given a filename generated by writeVectorToFile, we ...
2
votes
2
answers
163
views
In C is it possible to write a macro function to do a #ifdef check on a #define thats passed in?
I want to do something like this for many parameters that are passed in through "-DPARAM_NAME=x" during compile time:
#define CHECK_PARAM (PARAM_NAME,DEF_VALUE) \
Pseudo code :
#ifdef ...
0
votes
2
answers
90
views
Build a SAS macro based on date and existing data values so I can tell the program to run a slightly different series of steps based on conditions
I'm relatively new to SAS and very new to Macros, so please forgive me if this is a basic question, but I haven't been able to find answers elsewhere that have worked for me. Basically, I have a SAS ...
4
votes
1
answer
96
views
GNU as recursive/loop macro expected output
In this assembly file below, the macro jump_table should automagically create ... a jump table to consecutively numbered labels like jump_0, jump_1, ... jump_<n>.
It seems there is no loop ...
0
votes
1
answer
117
views
Redraw images in Libre Office Calc via basic macro
I want to write a macro which reloads all images inserted in a Calc document. I've heard of linking images, but I don't know the specific commands in LibreOffice Basic. So far the best answer I've got ...
2
votes
1
answer
119
views
Filtering macro/array data at compile time in C
Is there a way to filter macro or array data at compile time in C?
For example:
#define THRESHOLD 5
#define MACRO_DATA \
X( 1 ) \
X( 8 ) \
X( 3 ) \
X( 12 ) \
X( 5 )
// ...
1
vote
1
answer
155
views
How to call an inline method from within a scala 3.6.4 macro?
In a scala 3.6.4 serialization library I am developing there is the DiscriminatorCriteria type-class that allows the user to determine which discriminator value to use for each variant P of a sum-type ...
3
votes
2
answers
73
views
Lisp: Iterating over slots with a macro
With this:
(defclass test-class ()
((data :initarg :data)
(other :initarg :other)))
(defmethod test ((tc test-class) &key)
(macrolet ((print-slot (slot)
`(with-slots (,slot) ...
0
votes
1
answer
59
views
How to avoid calls to `scala.quoted.Quotes.reflectModule.reportModule.info` to be ignored in compiler output?
For debugging a scala 3.6.4 recursive macro I added many calls to quotes.reflect.report.info.
But only a few are shown in the compiler output despite all the messages are different.
Apparently, the ...
1
vote
1
answer
134
views
C++ automatic getter and setters using templates, reflection, and macros
I'm making a small library that will (among other things) allow users to provide a class, A as a template argument to a base class, Base, and have all the (public) class fields of A get converted into ...
0
votes
0
answers
49
views
Markov Chain prediction using Excel Macro - accuracy differs significantly in real time prediction and back testing
I have a macro for inventory replenishment prediction using Markov Chain. It uses data for last 10 days including today to predict inventory replenishment (rep R) or not (blank B).
The model works ...
1
vote
1
answer
103
views
In scala 3.6.4, how to call a polymorphic method with type bounds from inside a quoted expression?
The method to call is the SAM of the following type class:
trait DiscriminatorCriteria[A] {
def getFor[V <: A]: Int
}
And the headType to pass as type argument (to type parameter V) was ...
0
votes
1
answer
133
views
Using Notepad++ to automate modification of Excel document because cells contain **
I am working on modification of a translation document that utilizes references for duplicate strings.
I want to run a macro that finds the REF:x,
Sample Table
REF
en-US
REF:1
Acknowledge
**REF:x**
...
2
votes
2
answers
158
views
Is there a way to determine whether a type is not defined?
I have a type Type_##type dependent on a certain macro type_create:
#define type_create(type) { \
typedef struct { \
type* ptr, \
} Type_##type; \
and I have a few macros dependent on ...
1
vote
1
answer
128
views
How to create Clojure-style loop/recur macro using syntax-case in Guile Scheme?
I'm learning syntax-case macro system. This is what I have so far:
(define-syntax loop
(lambda (x)
(syntax-case x ()
[(_ (var val x ...) body)
(with-syntax ([recur (datum->syntax ...
1
vote
1
answer
137
views
Combine X macros
is it possible in C to combine lists of macros to generate an enum with every possible combination?
Example:
#define REMAP_LIST R(FOE) R(ANY) R()
#define STATUS_LIST P(TRY) P() P(ABORT)
#define ...