Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [macros]

A macro in computer science is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to a replacement output sequence (also often a sequence of characters) according to a defined procedure. Do not use this tag for questions regarding Microsoft's Visual Basic for Applications. VBA evolved from an early macro language, but is not a macro language itself.

Filter by
Sorted by
Tagged with
24 votes
3 answers
1k views

Formatted print without the need to specify type matching specifiers using _Generic

This code allows printing in C with a style similar to C++ ostream. By using _Generic() (see ...
chux - Reinstate Monica's user avatar
19 votes
3 answers
13k views

Generic C++ exception catch handler macro

I have this set of legacy C++ projects with a large number of public functions. At the start, none of those publicly exposed functions had try..catch insulation ...
Allbite's user avatar
  • 395
19 votes
1 answer
510 views

Suggesting "safe enum" class for ISO C

There are quite some questions and answers about how to make enum really type-safe and I didn't find a solution that ensures both type safety and valid values. So I ...
Felix Palmen's user avatar
17 votes
2 answers
5k views

Template vector struct in C11

This one review will be a little bit tricky: I was trying to implement a template Vector struct in C11. Of course, templates don't really exist in C11, so I used a ...
Morwenn's user avatar
  • 20k
17 votes
0 answers
1k views

Autotools detect C++ language standard support

I have been updating my build tools to optionally use autotools (autoconfig/automake/libtool etc.). As part of this change I have written a couple of M4 macros. This not being something I have done ...
Martin York's user avatar
  • 95.2k
15 votes
3 answers
5k views

On-the-fly destructors

Due to my needing to use C libraries inside C++, I kept finding myself wanting to be able to ensure that the free function got called, even if an exception got ...
Justin's user avatar
  • 3,295
15 votes
1 answer
1k views

Signed integer-to-ascii x86_64 assembler macro

I have implemented an algorithm which converts a signed integer to its ASCII equivalent string with the corresponding sign as a first byte. I have tested it on several numbers and it worked correctly. ...
LRDPRDX's user avatar
  • 831
13 votes
3 answers
1k views

Validity of macro for printf() call in a corporate environment

I wrote this program: #include <stdio.h> #define printS for(i=0;s[i];i++){printf("%c",s[i]);} int i; char s[] = "Hello World!\n"; void main() { printS; } ...
Magister Ludi's user avatar
12 votes
5 answers
5k views

Macro to run code once during the lifetime of the program

I'm using this little macro a lot: ...
CocoCoder's user avatar
  • 123
12 votes
3 answers
27k views

Endianness conversion in C

I have written a simple C header for converting the endianness of short integers and long integers. It uses the GCC macro ...
Jack Wilsdon's user avatar
  • 1,651
12 votes
1 answer
289 views

Lua bindings for FTGL (FreeType font rendering in OpenGL)

I wrote some Lua bindings for FTGL's C API. This works well enough, but I ended up with lots of macros, one for each Lua function signature. For example, LUD_NUMBER_NUMBER_TO_NUMBER creates a Lua ...
Dagg's user avatar
  • 4,531
12 votes
1 answer
2k views

Macro to build type declaration

These are some macros to help build a traits class (for the parser/printer classes I am building). Traits.h ...
Martin York's user avatar
  • 95.2k
11 votes
5 answers
740 views

Resolving a link - follow-up

Similar piece of code to that I recently posted as: Resolving a link I have another piece of code which cannot be as easily extracted out into a method: ...
Billy ONeal's user avatar
  • 1,470
10 votes
6 answers
5k views

Length of a C string, capped to some maximum

If I have a oneliner in C such as size_t maxSizeOf(const char *s) { return strlen(s) > M_MAX_SIZE ? M_MAX_SIZE : strlen(s); } would it be better to have ...
Tom's user avatar
  • 567
10 votes
1 answer
3k views

Const by default

Being a C++ developer, but also having been influenced by functional languages, I strongly believe that const should be the default for the vast majority of all ...
Pascal T.'s user avatar
  • 201
9 votes
2 answers
6k views

Macro that interchanges 2 arguments

Define a macro swap(t, x, y) that interchanges two arguments of type t.(Block structure will help.) The ideea is that a ...
cristid9's user avatar
  • 1,271
9 votes
1 answer
5k views

Variadic macro enum class "reflection" in C++11

I recently had the need of "reflecting" multiple enum class constructs in order to get their elements' names as std::string ...
Vittorio Romeo's user avatar
9 votes
2 answers
3k views

A tuple unpacking macro that approximates Python elegance

Motivation Although I love coding in C++, I sometimes yearn for the syntactic sugar of Python. C++11 has somewhat eased the pain by such beautiful analogies like this: ...
Nobody moving away from SE's user avatar
8 votes
5 answers
5k views

Macro for allocation in C

As we all know, the syntax of allocating memory is a bit clunky in C. The recommended way is: int *p; int n=10; p = malloc(n*sizeof *p); You can use ...
klutt's user avatar
  • 903
8 votes
2 answers
977 views

JVM bytecode instruction struct with serializer & parser

I am writing a java class file parsing & serialization library. As part of that i needed to implement a structure for the JVM bytecode instructions as well as come up with a way to parse & ...
p0sa's user avatar
  • 81
8 votes
3 answers
3k views

The perfect function alias

For many years, I have waited for function aliases in C++. They still aren't here. I've been using macros to generate wrapper functions. This is my best attempt at the perfect function alias: ...
Indiana Kernick's user avatar
8 votes
1 answer
513 views

Source code level portable C++ Unicode literals

Windows console windows do unfortunately not support stream I/O of international characters. For instance, in Windows 7, you can still do "chcp 65001" (sets the active code page to UTF-8), type "more",...
Alf P. Steinbach's user avatar
7 votes
4 answers
3k views

'do { statement; } while(0)' against 'statement' when writing C macro? [closed]

Which one of the following is preferrable and why when writing C macro? Type 1 #define I2C_START() I2C_WAIT_IDLE(); SSP1CON2bits.SEN = 1 Type 2 ...
Donotalo's user avatar
  • 224
7 votes
3 answers
8k views

Macro to generate an enum and an array of strings

Often when I find myself working with magic number enum values and I want to know what they represent, so I create an array of strings in order to print out their label. This macro automates that ...
Alex Angel's user avatar
7 votes
4 answers
893 views

Macro enabling Python style 'with' in C++

I am developing a simple macro that enables Python style 'with' in C++. I have shamelessly prefixed the macro name with BOOST_ - primarily because there's a ...
idefixs's user avatar
  • 173
7 votes
1 answer
373 views

Pseudo-Generic Array Stack in C

I have implemented an array based pseudo-generic stack in C using macros. The code works fine for all data types. Is it a good idea to implement such a data structure using macros? array_stack.h ...
DIVIJ_404's user avatar
7 votes
1 answer
477 views

Unit test macro

I have written a couple of macros (? and ??) for performing unit tests, but I'm having some difficulty with modifying it, so I ...
Sod Almighty's user avatar
7 votes
1 answer
203 views

Validation macro

I would like a review of a Scala validation library I am writing. For now we can focus on the regular expression component. The usage is described in RegexExample.scala There is an outline of the ...
user833970's user avatar
6 votes
2 answers
3k views

A simple error messaging and logging system via macro(s) in C++

I'm just trying to build a quick and crude error messaging system. It currently looks like this: ...
Francis Cugler's user avatar
6 votes
2 answers
8k views

Calendar for any given month/year

This is one of my first C++ training project where I created the console application which displays the calendar of any given month. I observed how offset of days (empty places on the calendar) ...
L_J's user avatar
  • 263
6 votes
2 answers
2k views

Format log with a small macro logger

The goal of this code is to format any log to STDOUT and STDERR with the file and the line of where the call is made. I took ...
bl4ckb0ne's user avatar
  • 253
6 votes
2 answers
2k views

Solving the problem of using directives in a header file with a macro. Is this stupid?

I am writing some library code that is mostly templates and so is all contained in header files. I know that placing a using declaration in a header will pollute all the files that include it, but I'm ...
Roald's user avatar
  • 161
6 votes
1 answer
273 views

Using get-setf-expansion

In a previous post at Simplifying complex setf expressions, @sds suggested using the get-setf-expansion macro for implementing place modification, as discussed in ...
davypough's user avatar
  • 405
6 votes
2 answers
2k views

Generic vector implementation in C using macros

I while ago I experimented with macros in C and came up with the idea of implementing a generic vector library using macros. This code uses the non standard typeof ...
wefwefa3's user avatar
  • 163
6 votes
1 answer
216 views

C++: Generating similar methods with macros

I am currently working on a project that involves Lua. For convenience, I created a wrapper class for the Lua state structure. However, I also added some methods for getting globals and table fields. ...
Tobyte M.D.'s user avatar
5 votes
3 answers
2k views

Fastest FIFO with macros for use embedded devices

UPDATE (a year later) Since this post the code has turned into a small library called fifofast and is hosted on github under the MIT License. This note is meant for anyone who stumbled across this ...
nqtronix's user avatar
  • 161
5 votes
2 answers
346 views

C program to generate Seed programs from statically defined Befunge code

My program takes static input, and then number crunches for a few hours. I am currently using Macros, but is there a better, more "user friendly" way to do this without losing on performance. I have ...
wizzwizz4's user avatar
  • 323
5 votes
2 answers
377 views

C Macro Generic Linked List

For a recent project of mine I had to create various data structures in pure C with no external libraries (in other words, code I've written by myself), which lacks the templating and OOP capabilities ...
404 Name Not Found's user avatar
5 votes
1 answer
234 views

C++ extended switch for any class

Was thinking about extending the C switch beyond integers. Here is what it came to - will gladly accept any suggestions for improvement. Note: "Any class" in the title is not quite correct - ...
johngo's user avatar
  • 51
5 votes
3 answers
1k views

Encapsulating snprintf to avoid repetition of sizeof

Correct usage of snprintf involves very long and repetitive lines: ...
alx - recommends codidact's user avatar
5 votes
3 answers
85 views

Four versions of a database-creation function with different error handling

I'm starting a new, small C project and would like to have the safest, cleanest and shortest error handling code possible. Here is a comparison of different ways to handle errors in the same C ...
Julien__'s user avatar
  • 339
5 votes
1 answer
164 views

Autotools detect YAML library

I have been updating my build tools to optionally use autotools (autoconfig/automake/libtool etc.). As part of this change I have written a couple of M4 macros. This not being something I have done ...
Martin York's user avatar
  • 95.2k
5 votes
0 answers
35 views

Procedural macro to derive de/serialization trait from/to little endian bytes

I wrote a trait library that allows the de-/serialization of data structures from/to little endian bytes 1. For it I now wrote my first procedural macro 2 to be able to derive the traits ...
Richard Neumann's user avatar
4 votes
2 answers
2k views

Macros for bitsets / bit-flags in C

Context: I have some C code that uses a lot of bit-flags. The flag-sets may have a couple of hundred members. Currently these flags are defined as a 32-bit mask that must be applied to a specific ...
user673679's user avatar
  • 11.8k
4 votes
3 answers
835 views

Use of macros to aid visual parsing of SFINAE template metaprogramming

I've recently been introduced to SFINAE to solve the problem of unwanted promotion precedence. i.e. I was hoping to catch integer types with Foo::Foo(long) and ...
P i's user avatar
  • 629
4 votes
2 answers
381 views

C++ (Cross-Platform) Predefined Library

As we know there is no standard way in compilers to standardize macros with compilers and different operating systems So I created this library that contains standardized compiler, os, attributes ...
Maysara Elshewehy's user avatar
4 votes
3 answers
280 views

Penney Game - mapping macros to strings

The SPOJ problem: Given a 40 character string representing outcomes of 40 coin tosses, find the frequency destribution of all the possible outcome triplets. So, for string like : HHHH....40 Hs ,...
user43115's user avatar
  • 329
4 votes
1 answer
2k views

Check for iOS version

Here's an approach i came up with for checking IOS versions. ...
damote's user avatar
  • 141
4 votes
2 answers
2k views

A defined macro to copy selected values of std::vector to an array using std::copy

Thought I share this piece of code to the world. But be aware, I am not sure if this piece of code is safe and efficient. Feel free to improve it or give some feedback and suggestions. ...
CLearner's user avatar
4 votes
1 answer
177 views

Custom derive macro to create getters and setters

To learn how custom derive macros work in Rust, I decided to create a simple derive macro to provide getters and setters for a given struct. It would be used like this: ...
Dornteufel's user avatar