Skip to main content
19 votes
Accepted

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

Sure, if you're OK with using macros in the first place, then defining a parametrized one rather than keep repeating the same conditional code is certainly preferable by any measure of good coding. ...
Kilian Foth's user avatar
15 votes

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

You are confusing two different concepts in your question. Macros are not about compiling code at runtime. They are the exact opposite: they are about running code at compile time. So, in this case, ...
Jörg W Mittag's user avatar
14 votes

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

There's an element of personal preference here, but in C++ I prefer to do this in the header file: #ifdef _DEBUG void DebugMessage(...); #else inline void DebugMessage(...) {} #endif So that ...
Jack Aidley's user avatar
  • 2,952
12 votes

Is updating a macro value in Xcode preprocessors marcos violating open closed principle?

If your requirements have permanently changed, just change the code already. Anything else - including slavish devotion to the open-closed principle - is abstraction for abstraction's sake and does ...
Philip Kendall's user avatar
12 votes
Accepted

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

Macros have the advantage to be expanded at compile time The idea of Lisp macros is to be able to fully expand them at compile time. Then no compiler is needed at runtime. Most Lisp systems allow you ...
Rainer Joswig's user avatar
12 votes
Accepted

Writing a #define for a common statement

Don't do this. Using macros to reconfigure the language like this is like writing in slang. One or two instances might not seem so bad to someone has to read it (including yourself) but every time ...
UserNotFound's user avatar
11 votes

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

I currently work on a code base that has classes created with these types of macros. I would strongly discourage doing things this way because if anything goes wrong in the future with any of the ...
user1118321's user avatar
  • 4,991
9 votes

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

A typical "compiling lisp" will include the compiler in a bundled image. Furthermore, most (although not all) function calls are done through symbol indirections (basically, when the compiler sees (+ ...
Vatine's user avatar
  • 4,269
8 votes
Accepted

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

I oppose to use macros instead of normal class here because: It is harder to debug, especially when it has compile error If the cost of creating a new class easily is harder to maintain later, I ...
ocomfd's user avatar
  • 5,712
7 votes

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

Using macros for this will become a maintenance horror, as others have already pointed out. As an alternative approach, you could implement a small code generator for creating the repeated boilerplate ...
Doc Brown's user avatar
  • 209k
7 votes

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

The advantage of hygienic macros is not one of language capability -- you can write macros that have good hygiene using gensym and careful quoting/unquoting at the right times. However, hygienic ...
Nathan Davis's user avatar
7 votes

Is updating a macro value in Xcode preprocessors marcos violating open closed principle?

First, make sure you have understood that the OCP is not violated at the time when you change some source code, see my answer here for a detailed explanation. The OCP is followed or violated at the ...
Doc Brown's user avatar
  • 209k
6 votes
Accepted

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

Today, the preprocessing is actually happening inside the compiler (e.g. inside the cc1plus executable started by g++ command). Use g++ -C -E to get the preprocessed form. Preprocessing and parsing ...
Basile Starynkevitch's user avatar
6 votes
Accepted

How to stay DRY with return values

This could be refactored in several ways with varying levels of elegance and intrusiveness. Do repetitive things in a loop The idea is simple: Put your values into arrays and use a loop to retrieve ...
5gon12eder's user avatar
  • 7,206
5 votes
Accepted

Using a macro for a libraries namespace?

This technique is sometimes used to handle transitive dependencies on header only libraries. It is not generally a best practice. The problem: I am writing a C++ library. I put all of my declarations ...
amon's user avatar
  • 135k
4 votes

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

Clearly, runtime code generation is incompatible with ahead-of-time compilation. Therefore, the language runtime environment must include some mechanism to dynamically execute code: either an ...
amon's user avatar
  • 135k
4 votes

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

Yes. The runtime have to include an interpreter or compiler. This is why eval is traditionally is a feature of interpreted languages, since the runtime of these languages (by definition) contains an ...
JacquesB's user avatar
  • 59.8k
4 votes
Accepted

Declaring functions using macros?

The way macros are used makes it very difficult for a programmer to reason about them, and guess what would be the result of a macro. For anything but the most basic examples, macros would usually ...
Arseni Mourzenko's user avatar
4 votes

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

For trivial examples, any form of additional overhead will appear massive. The benefit of separating into TUs is that you know that everything in the file is specific for the platform and you can use ...
Lars Viklund's user avatar
  • 2,136
4 votes

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

Most Probably Not. Lisp Macros appear to have two big purposes. First is the DRY ("don't-repeat-yourself") aspect, enabling the capture of patterns that would otherwise be difficult-or-impossible in ...
HostileFork says dont trust SE's user avatar
4 votes

Current industry standard with regards to C macros

You see an absolute rule, and things don't work that way. You also seem to be focussed on macros vs. inline functions which is very rarely the question. Macros can be extremely useful if you know how ...
gnasher729's user avatar
  • 45.9k
3 votes

Writing a #define for a common statement

To answer your question, yes you could write a macro to accomplish the for loop. However, this is not a very good idea. As another user pointed out, this adds difficulty in understanding the program. ...
Snoop's user avatar
  • 2,738
3 votes

How useful are Lisp macros?

Here's one I use for debugging (in Clojure): user=> (defmacro print-var [varname] `(println ~(name varname) "=" ~varname)) #'user/print-var => (def x (reduce * [1 2 3 4 5])) #'user/x => (...
Jonas Kölker's user avatar
3 votes
Accepted

Using a preprocessor macro to wrap functions?

Does this work? Yes, probably. By using variadic macros, the usual errors about commas in the arguments will not occur. However, this approach might not be very good for debuggability – this will ...
amon's user avatar
  • 135k
3 votes

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

In general, if you're not programming on the bare hardware, your application is talking to the operating system and has to believe what it is told, even if the OS claims "this is actually what the ...
Hans-Martin Mosner's user avatar
2 votes

Python decorators and Lisp macros

A decorator (be it in Python or in any other - functional programming - language) is just a function which accepts an original (to-be-decorated) function (and sometimes more additional arguments), and ...
Gwang-Jin Kim's user avatar
2 votes

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

Definitely, just make sure you're not stepping on the code guidelines given by your team. Make sure no other code in the system tries to reach the same functionality via a general if condition.
James O's user avatar
  • 21
1 vote
Accepted

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

Generally, no. Even if you're going to write a USB HID mouse/keyboard driver and your program somehow have the privilege to replace the default mouse/keyboard driver in the kernel, or even if your ...
Lie Ryan's user avatar
  • 12.4k
1 vote

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

class MyClass : public AbstractBase { Q_OBJECT You're not writing C++ code here, you're writing Qt code. Critically, this is code that is first processed by the Qt moc compiler. It expects to ...
MSalters's user avatar
  • 8,812

Only top scored, non community-wiki answers of a minimum length are eligible