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.[Tag:vba] evolved from an early macro language, but is not a macro language.

Filter by
Sorted by
Tagged with
19
votes
1answer
359 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 ...
17
votes
3answers
10k 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 ...
16
votes
2answers
432 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 ...
15
votes
2answers
3k 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 ...
14
votes
3answers
3k 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 ...
13
votes
0answers
160 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. ...
13
votes
0answers
471 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 ...
12
votes
3answers
8k 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 ...
12
votes
1answer
235 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 ...
12
votes
1answer
1k 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 ...
11
votes
3answers
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; } ...
11
votes
5answers
4k views

Macro to run code once during the lifetime of the program

I'm using this little macro a lot: ...
9
votes
6answers
1k 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 ...
9
votes
5answers
719 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: ...
9
votes
2answers
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: ...
8
votes
2answers
4k 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 ...
8
votes
1answer
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 ...
8
votes
1answer
317 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",...
8
votes
1answer
818 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 ...
7
votes
4answers
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 ...
7
votes
3answers
498 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: ...
7
votes
4answers
381 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 ...
7
votes
1answer
220 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 ...
7
votes
1answer
188 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 ...
6
votes
5answers
1k 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 ...
6
votes
2answers
3k 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) ...
6
votes
2answers
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 ...
6
votes
1answer
133 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 ...
6
votes
1answer
190 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. ...
5
votes
3answers
1k 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 ...
5
votes
2answers
746 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 ...
5
votes
3answers
158 views

Encapsulating snprintf to avoid repetition of sizeof

Correct usage of snprintf involves very long and repetitive lines: ...
5
votes
3answers
79 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 ...
5
votes
1answer
127 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 ...
4
votes
2answers
71 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 ...
4
votes
1answer
2k views

Check for iOS version

Here's an approach i came up with for checking IOS versions. ...
4
votes
3answers
258 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 ,...
4
votes
2answers
1k 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. ...
4
votes
2answers
145 views

Efficient mechanism to generate operator overload functions

I'm attempting to tidy up a C++ framework (PyCXX). One particular file contains ~400 lines of operator overload functions, which I've managed to reduce to the following: ...
4
votes
2answers
252 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 ...
4
votes
2answers
252 views

Macros to detect UTF-8

I'm working on a program that handles UTF-8 characters. I've made the following macros to detect UTF-8. I've tested them with a few thousand words and they seem to work. I'll add another one to do ...
4
votes
1answer
508 views

AppleScript to close running processes

I've been trying to emulate MS Windows behavior on my OS X and close processes that do not have a window. What I'd really like to do is "quit" the process on clicking the red "x" button. Instead, I'...
4
votes
2answers
121 views

Keys in mode maps

I noticed a pattern in some elisp modes I was putting together: (let ((map (make-sparse-keymap))) (define-key map KEY 'FN) ... (setq FOO map)) so I wrote ...
4
votes
2answers
69 views

Error-checking macro to jump to specified label given a failing statement

I'm starting a new C projet and I want to have the cleanest and shortest error checking code possible. To that aim, I often use two things : functions return 0, ...
4
votes
1answer
82 views

Introduce bindings for macro user

I'm doing the Racket track on exercism.io and solved the grains exercise: Write a program that calculates the number of grains of wheat on a chessboard given that the number on each square doubles. ...
4
votes
1answer
96 views

Scale-able API development with macro definitions

In a couple of months I'll be part of a large(ish) project, and there's good chances my team will be a lot bigger, with at least 2 programmers (probably 3) and some 3-5, or more, consumers of the API ...
4
votes
1answer
790 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 ...
4
votes
1answer
4k views

Qt foreach-like alternative to iterate over value AND key of an associative container

The Qt documentation recommend an iterator-based solution to iterate over an associative container like QMap and QHash, and I always wondered if there really isn't a (nice) solution using a ...
4
votes
1answer
187 views

Is this a good way to implement let-with?

I've implemented a let-with macro that takes an association list, keyed by symbols, that enables code to bind their corresponding values to local variables. It's ...
3
votes
3answers
616 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 ...