0

In this recent question and many similar questions, there is ultimately a query like, "How do I generate {code} using the preprocessor?"

The code in the question linked above is a perfect candidate for something like GNU AutoGen, or even something simpler like unmarshaling the XML, YAML, JSON, or other format and then marshaling to the desired code.

Simplistic X-macros are one way to generate code, and it's arguably useful in simple situations. However, some people seem to want to apply a similar mindset to more complex problems that require non-obvious uses of the preprocessor when it would be a lot faster to simply generate the code themselves than to wait for answers to their specific situation.

Is there a reason to increase preprocessing times (and compilation times as a result)? Alternatively, is there a reason why relying on an external tool—whether it's a full template processing tool, a small helper C program compiled and executed on the host system, or even a simple shell script—to generate the code is less advantageous during development?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Aren't the answers / trade-offs self evident? Cost of building / maintaining a custom generator versus quality (e.g. performance) of the code that it generates. – Stephen C Jun 11 '17 at 02:58
  • 1
    This is very opinion-based, and probably would get better answers on https://softwareengineering.stackexchange.com/. – Pokechu22 Jun 11 '17 at 03:07

1 Answers1

2

Is there a reason to increase preprocessing times (and compilation times as a result)?

In practice the preprocessor is fast. But it is not very powerful, and tricks like this are not very readable.

Alternatively, is there a reason why relying on an external tool—whether it's a full template processing tool, a small helper C program compiled and executed on the host system, or even a simple shell script—to generate the code is less advantageous during development?

I believe that most people don't think enough on relying on external tools. But they have several downsides; first, external tools add some external dependency. Then, many external tools don't generate reliable #line directives, so error messages are less readable. At last, some build automation tools are difficult to configure for external tools: adding a make rule in a Makefile is easy, but configuring some IDE for them could be harder.

(your question is IMHO very opinion based)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you for your answer. I actually knew of the Boost Preprocessor library and failed to find the reason for its existence before asking. Originally I thought to include C++ as my question also applies to it, but it would invite answers related specifically to C++, discouraging answers applicable to both languages. However, many of the answers you provide are also expressed in the [Motivation documentation for Boost's Preprocessor library](http://www.boost.org/doc/libs/1_64_0/libs/preprocessor/doc/topics/motivation.html). Had I found that last night, I wouldn't have asked. Thanks again –  Jun 11 '17 at 17:49