Comment by WalterBright
4 years ago
To clarify, what is needed are:
1. static if conditionals
2. version conditionals
3. assert
4. manifest constants
5. modules
I occasionally find macro usages that would require templates, but these are rare.
4 years ago
To clarify, what is needed are:
1. static if conditionals
2. version conditionals
3. assert
4. manifest constants
5. modules
I occasionally find macro usages that would require templates, but these are rare.
One other thing that would be great that sometimes people use the preprocessor for is having the names variables/enums as runtime strings. Like, if you have an enum and a function to get the string representation for debug purposes (i.e. the name of the enum as represented inside the source code):
you can use various preprocessor tricks to implement getEnumName such that you don't have to change it when adding more cases to the enum. This would be much better implemented with some compiler intrinsic/operator like `nameof(val)` that returned a string. C# does something similar with its `nameof`.
> you can use various preprocessor tricks to implement getEnumName such that you don't have to change it when adding more cases to the enum.
For those who don’t know: the X Macro (https://en.wikipedia.org/wiki/X_Macro, https://digitalmars.com/articles/b51.html)
Hey, even an article written by Walter, that's a fun coincidence! :)
This is slightly different than the form I've seen it, but same idea: in the version I've seen, you have a special file that's like "enums.txt" with contents like (warning, not tested):
and then you write:
Same idea, just using an #include instead of listing them in a macro. Thinking about it, it's sort-of a compile time "visitor pattern".
1 reply →
I like that ONE == 0.
Did not even think about that :) Just so used to thinking of enums like that as opaque values.