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
12 votes

Is updating a macro value in the Xcode preprocessor's macros violating the 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
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,981
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,760
7 votes

Is updating a macro value in the Xcode preprocessor's macros violating the 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
  • 220k
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
  • 220k
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
  • 136k
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
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
  • 136k
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
  • 49.4k
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
3 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.5k
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
  • 9,058

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