Questions tagged [macros]

Macros are used to make a sequence of computing instructions available to the programmer as a single program statement, making the programming task less tedious and less error-prone.[

Filter by
Sorted by
Tagged with
3 votes
1 answer
678 views

Using a preprocessor macro to wrap functions?

I have multiple use cases where I need to call an "entry" function, do a bunch of stuff, and then call an "exit" function. (In my case, "entry" is enabling a chip select ...
-5 votes
2 answers
161 views

Current industry standard with regards to C macros [closed]

I have read from several authors that macros in C should be avoided whenever possible, and use inline functions instead. It's true that inline only 'requests' the compiler to replace the function call ...
  • 615
-1 votes
2 answers
125 views

Detecting keyboard "callbacks" directly from a keyboard not from a operating system

So, I am making a pure 2D shooter in Unity, and I was thinking about cheats a little bit (Yes, I know that nobody is going to play my game but anyway). I was wondering if you can get pressed keys ...
13 votes
3 answers
2k views

Is using C/C++ macros as a shortcut for conditional compilation a good practice?

Let's say I want to have several types of output messages in my code. One of them is DEBUG, which is printed only, when the code is compiled in Debug mode. Usually I'd have to write something like #...
  • 327
6 votes
4 answers
5k views

Is this using macros to define classes that fit a pattern in C++ a sound idea?

I've got a set of classes that all inherit from a base class that are responsible for different functions (sort of like a group of "operators") They all work on the same input and output the same ...
  • 1,232
1 vote
1 answer
5k views

Using a macro for a libraries namespace?

In some code bases (such as hydra, and thrust's tuple implementation) I see namespaces defined entirely with macros. It appears the reason for this is so that you can configure the namespace to your ...
  • 1,232
3 votes
1 answer
4k views

Declaring functions using macros?

Is it generally encouraged or discouraged to create a pre-processor macro in C, that facilitates the creation of many functions with the same signature (differing only in name). For instance, instead ...
0 votes
1 answer
116 views

Time profiling - is using macros bad?

I need to make a time profiling for several modules in Fortran, which means, that I'm supposed to write the same code in every beginning and every end of every function. Really, it looks like this: ...
  • 327
11 votes
7 answers
3k views

How can a compiler be written for a language that allows rewriting code at runtime (such as Lisp macros)?

There are some programming languages, like the many dialects of Lisp, that allow for macro-metaprogramming: rewriting and altering sections of code before the code is run. It is relatively trivial to ...
  • 4,589
2 votes
3 answers
279 views

Writing a #define for a common statement

Often when I program in C I write the expression for (int i = 0; i < j; i++). I never wrote a macro or a define expression, but could I write a #define to simplify the above expression so that do ...
1 vote
1 answer
703 views

when writing platform specific code, is using separate .cpp always preferred instead of using #ifdef?

I have some platform specific code, such as a string needs different values at different platforms like it: test.cpp #if(PLATFORM==ANDROID) string url="android"; #elif(PLATFORM==IOS) string ...
  • 5,725
2 votes
1 answer
2k views

Does having so many macros (#define) increase compilation time due to prolonged pre-processing?

In a very simplistic way, I understand: "Compilation" = "Pre-processing" + "Parsing" + "Linking" + "Executable" All the macros and other such pre-processing directives are taken care at the "Pre-...
  • 2,232
3 votes
2 answers
337 views

How to stay DRY with return values

I have a bunch of repetitive C++ code that looks like this: // Compute finalOutput if possible. Return true if successful, false otherwise // finalOutput only holds a valid value if true is returned. ...
32 votes
2 answers
2k views

What is the origin of the C Preprocessor?

The C preprocessor is attached to C, but it has a completely different syntax from the main language: syntactically significant whitespace (end of line terminates a statement, gap after the macro ...
12 votes
1 answer
2k views

What practical problem results from lack of hygienic macros in Clojure?

I've heard that Clojure macros are easier to write but not as reliable as Racket's hygienic macros. My question has 2 parts: How does gensym differ from hygienic macros? What do Racket macros provide ...
  • 284
7 votes
3 answers
1k views

Would Rebol (or Red) benefit from Lisp-style Macros?

As a 'seasoned' Rebol developer with some knowledge of the world outside, I'd be curious as to the utility/pitfalls of implementing Lisp-style macros in Rebol (and/or Red). My understanding (always ...
  • 365
7 votes
3 answers
1k views

Idiomaticy of macros in C++

Macros are considered a good thing by one and evil by another. Is there a rule of thumb when to and when not to use macros in C++? When are macros idiomatic and when should they be avoided?
  • 191
21 votes
4 answers
3k views

What about LISP, if anything, makes it easier to implement macro systems?

I'm learning Scheme from the SICP and I'm getting the impression that a big part of what makes Scheme and, even more so, LISP special is the macro system. But, since macros are expanded at compile-...
5 votes
1 answer
3k views

Using macros to protect assignment to global variables

Because there is no language feature in C to protect assignment to global variables would you recommend doing something like this? Take this example: We have a module with the header file called ...
  • 997
5 votes
1 answer
1k views

Using macros to implement a generic vector (dynamic array) in C. Is this a good idea?

So far I have only done personal projects at home. I hope to get involved in some open source project some time next year. The languages I that have been using the most are C and C++. I have used both ...
  • 997
4 votes
1 answer
1k views

Are macros values, data types, identifiers, variables, constants, or ...? [closed]

Macros exist in many programming languages, for example, C. A data type is a set of values with operations on the values. Are macros values, or data types? Are macros identifiers, variables, or ...
  • 5,365
0 votes
1 answer
729 views

Macro guard in Objective-C

I have noticed that many famous libraries written in Objective-C (eg. AFNetworking) use the macro guard inside their header files. Im aware that the #import directive, which works exactly like #...
  • 103
1 vote
1 answer
335 views

What is this programming style? "Monoid-ic"?

In a moderately old blog post, Conal Elliot makes an interesting (if less than serious) argument that C is a purely functional language, by drawing a parallel between the combination of the C ...
7 votes
1 answer
1k views

Is it fair to say that "macros don't compose"?

On this blog post aphyr (who is a brilliant programmer) states: Clojure macros come with some important restrictions. Because they’re expanded prior to evaluation, macros are invisible to functions....
  • 4,809
0 votes
2 answers
644 views

Is it acceptable to use C11 generic macros in Objective-C to box numbers?

I was getting tired of repeating types when writing things like this: NSDictionary* d = @{@"so": [NSNumber numberWithInt:index]), @"much": [NSNumber numberWithBool:accepted]), ...
20 votes
4 answers
4k views

Python decorators and Lisp macros

When looking Python decorators someone made the statement, that they are as powerful as Lisp macros (particularly Clojure). Looking at the examples given in PEP 318 it looks to me as if they are just ...
3 votes
3 answers
2k views

Does macros support make Scala a Lisp dialect?

I've read recently that macro support in Scala is now official. I checked the documentation page and they are reminiscent to the LISP ones. In one of his essays Paul Graham writes that when "you add ...
  • 1,190
8 votes
3 answers
3k views

When would I require a Macro instead of a function?

I am new to Clojure, I am new to Macros and I have no prior background in Lisp. I went on to create my own switch case like form and ended up with this: (defmacro switch-case [v cases default] (if (...
3 votes
2 answers
5k views

Why can't we declare 'extern C' for C++ macro's?

I am new to C/C++. Wanted to know why we cannot declare 'extern C' for C++ macro's similar to methods/functions...which will allow macro's defined in C++ file to be accessed in .c files. Thanks in ...
  • 391
10 votes
3 answers
1k views

Byte code weaving vs Lisp macros

I have been reading about the libraries people have written for languages like Java and C# that make use of byte code weaving to do things like intercept function calls, insert logging code, etc. I ...
  • 1,603
2 votes
2 answers
1k views

Besides macros, are there any other metaprogramming techniques? [duplicate]

Possible Duplicate: Programming languages with a Lisp-like syntax extension mechanism I'm making a programming language, and, having spent some time in Lisp/Scheme, I feel that my language should ...
  • 129
20 votes
19 answers
6k views

Programming languages with a Lisp-like syntax extension mechanism [closed]

I have only a limited knowledge of Lisp (trying to learn a bit in my free time) but as far as I understand Lisp macros allow to introduce new language constructs and syntax by describing them in Lisp ...
1 vote
1 answer
427 views

Is there a way to compare and record changes to code without VCS?

I'm working for a client that has no version control system in place. I want to record changes to code without having to add all changes to a spreadsheet. I'm restricted to using Notepad++ so would a ...
  • 699
20 votes
1 answer
6k views

Why does the C library use macros and functions with same name?

I am reading 'The Standard C Library' by PJ Plauger which is really interesting. The book explains not only how to USE the library but also how it is implemented. I have finished reading the ctype.h ...
  • 1,757
6 votes
1 answer
354 views

Lisp Macros: A practical approach

On my way to learn Lisp I have discovered the all powerful and feared so called Macros, then after spending a hard time trying to understand them and their usefulness I said to myself, I FINALLY GOT ...
  • 161
20 votes
2 answers
2k views

What are the typical applications of Lisp macros?

I am trying to learn some LISP and I have read a lot about the importance of LISP macros so I would like to get some working experience with them. Can you suggest a practical application area that ...
  • 19.3k
-4 votes
2 answers
930 views

Why #pragmas are not part of the C++ standard?

Usually the problem of having possible multiple inclusions is solved with a series of #ifdef #ifndef but the pragmas just solves this with a single line, apparently they are really useful and can make ...
  • 1,175
2 votes
2 answers
2k views

Using macro as an abstraction layer

I am having a discussion with a colleague about using macro as a thin (extremely) layer of abstraction vs using a function wrapper. The example that I used is Macro way. #define StartOSTimer(period) ...
  • 1,469
2 votes
3 answers
213 views

What do you use macros in your editor for? [closed]

My editor of choice is Notepad++. It has macro capabilities, but as much as I think, I don't see how can I use it for anything. Do you use macros in your editor that have made your work easier? (...
  • 335
4 votes
1 answer
2k views

Why no more macro languages?

In this answer to a previous question of mine about scripting languages suitability as shells, DigitalRoss identifies the difference between the macro languages and the "parsed typed" languages in ...
24 votes
6 answers
6k views

How useful are Lisp macros?

Common Lisp allows you to write macros that do whatever source transformation you want. Scheme gives you a hygienic pattern-matching system that lets you perform transformations as well. How useful ...
  • 1,387
30 votes
5 answers
17k views

Are C++ templates just a kind of glorified macros?

From different comparisons among C++ templates and C#/Java generics like this one- https://stackoverflow.com/questions/31693/what-are-the-differences-between-generics-in-c-and-java-and-templates-in-c/...
  • 9,352
10 votes
3 answers
2k views

Is macros support in a programming language considered harmful?

The first abuse that comes to my mind in C is: #define if while But at the same time it is extremely handy and powerful when used correctly. Something similar happens with Common Lisp macros. ...
  • 1,675
41 votes
7 answers
13k views

Why aren't macros included in most modern programming languages?

I know that they are implemented extremely unsafely in C/C++. Can't they be implemented in a safer way? Are the disadvantages of macros really bad enough to outweigh the massive power they provide?
  • 7,632