4 ms·
Metaprogramming should be limited, and metaprogramming in C is quite brittle and almost nobody really understand the execution model of he preprocessor. But, di
by jstimpfle 4y ago
Metaprogramming should be limited, and metaprogramming in C is quite brittle and almost nobody really understand the execution model of he preprocessor. But, dismissing the preprocessor outright is too quick a conclusion. There are some clever and very practical uses of the preprocessor.
- portability
- generate strings from identifiers
- Insert file and line of the call location (or other context) automatically when calling a function.
- X-macros
- Scopes with exit statement given at beginning,
hacked using "for" statement and a condition that
evaluates to true only the first time. A little bit
brittle when the exit statement must not be skipped (goto, return).
I'm sure there are more, but I'm too tired to think harder... Here is an example of the last category:
#define TIMED_SECTION_(name, t) FOR_SCOPE(Timer t = start_timer(); print_passed_time(name, t))
#define TIMED_SECTION(name) TIMED_SECTION_(name, UNIQUE_NAME())
Not giving the FOR_SCOPE() and UNIQUE_NAME() macros here for brevity. They add another 3-5 reusable lines. Use like this:
TIMED_SECTION("foo the bar")
{
/* Do stuff, time spent in this scope will be measured and printed on scope exit. */
}
Other more advanced languages can do some of the same things, sometimes more robustly, or not at all (X-macros is probably hard to replace for languages that are not LISP). But it typically comes at a cost in complexity, hard to use type systems, complicated semantics with edge cases... And they only allow you to do what is already built into the language!
- kaba0 4y agoI don’t know, I would much rather prefer a sane metaprogramming system instead of this unhygienic mess of a hack.
- jstimpfle 4y agoWouldn't we all. Now show me how to do one that can do all the same things.
- comma_at 4y agohttps://github.com/eudoxia0/cmacro https://github.com/eudoxia0/cmacro
- kaba0 4y agoC++’s templates, const functions, etc. Rust’s macro system. C just refuses to improve, even though there are a huge space for those.
- jstimpfle 4y agoAll these come with additional complexity on the syntactic as well as semantic levels. C++ templates are an interesting in-between, between solid and "soft" structure. Not dissimilar to hygienic Lisp macros I think. But all this stuff multiplies complexities of using the language as well as compiler design. Not saying that other approaches are bad, just that C has some qualities that are underappreciated, or simply not understood by many. In case of the preprocessor, while it's certainly flawed, it's good that it's a mechanism that is completely orthogonal to the language. Which means it doesn't interact with the normal syntax and semantics of the language, isn't a complexity multiplier. There is a single, well-chosen interaction point: the lexical syntax (token stream). This makes it hard or impossible to implement certain user-defined extensions in a robust way, but also allows you to do many important tasks of factoring out syntactic boilerplate just fine. And you can do some extremely practical hacks with it that other systems just don't let you do at all. In any case, the cost of using it is very "self-contained".
- kaba0 4y ago> it's good that it's a mechanism that is completely orthogonal to the language I don’t know, doing what equals to find and replacing string inside a Programming language (with a bit exaggeration on my part) seems like a terrible terrible idea, even just for the very trivial reason of outputting paired number of brackets. It is also terrible from a readability and maintainability point of view with quite a few insane hacks of hacks done to implement basic functionality in other languages (there is an infamous macro in the linux kernel that is so cleverly hacky that it is on another level). And all that for nothing, as the language is incapable of expressing even such a basic thing as a generic array/vector-like data structure without a huge indirection, let alone more complex things. Minimalism is a nice goal, but without abstractibility/expressivity it is useless.