Earlier quoted context omitted.
Only if you can switch to something like Zig, Rust, etc. But a tremendous amount of code bases are already written in plain C. Using preprocessor metaprogramming allows you to invoke macros right in the same files in which you write ordinary C code, without the need for integration with other languages.
To date, C's most proximate (management-friendly, technically similar, programmer-skill-adjacent) competition has been c++. (I'm trying to lay the groundwork for the next statement: I'm not claiming this is good, and excited to see Rust and friends as serious contenders. Stay with me.) Even in c++20, there are things you can do with macros you can't do in the language. This may change when reflection lands in c++2x,…
Macros on Steroids: How pure C can benefit from metaprogramming
41–50 of 63 posts
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#42Earlier quoted context omitted.
To date, C's most proximate (management-friendly, technically similar, programmer-skill-adjacent) competition has been c++. (I'm trying to lay the groundwork for the next statement: I'm not claiming this is good, and excited to see Rust and friends as serious contenders. Stay with me.) Even in c++20, there are things you can do with macros you can't do in the language. This may change when reflection lands in c++2x,…
When I look at a lot of things that are done with reflection in C# I personally would much prefer the preprocessor and macros. Things that could be done in a type safe manner with a macro in a few lines become way more complex when using reflection.
Back when I was working in C#, there were quite a few times when I'd encounter things being done with reflection that I wish had been done with less code and a more type safe manner by just... not using reflection. I always just assumed a lot of that was just bad habits left over from the .NET 1.0 days, back before generics. So I'm curious if there's much overlap there? If it's only a subset, I don't know C as well as I'd like, so it would be interesting to see something that the C preprocessor handles better.
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#43Earlier quoted context omitted.
I humbly agree unless, of course, there is no other solution, e.g. lack of compilers on your platform.
Even so, I used to do a lot of metaprogramming with the C preprocessor back in the day. Over time, I grew disillusioned with it and gradually removed it from my C code. It just wasn't worth it. P.S. I also did metaprogramming in C by writing C programs to generate C code, such as converting an array of structs to multiple arrays, one for each field (for efficiency). This worked out fairly cleanly, but with D's abilit…
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#44You could create this as a frontend macro preprocessor that implements new language constructs. Maybe it could be called cfront. I just hope that the usefulness of that doesn't lead to bloated and complex codebases and binaries as features are added to the preprocessor.
There are several reasons why not to resort to third-party code generators [1]. This includes the burden of maintenance (think about the build process) and seamless integration of native macros with other code. Do you have some code to show bloated binaries with Datatype99/Interface99? The last time I checked the generated assembly code, it was nearly of the same size as hand-written code [2]. [1]: https://github.com…
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#45Earlier quoted context omitted.
Even so, I used to do a lot of metaprogramming with the C preprocessor back in the day. Over time, I grew disillusioned with it and gradually removed it from my C code. It just wasn't worth it. P.S. I also did metaprogramming in C by writing C programs to generate C code, such as converting an array of structs to multiple arrays, one for each field (for efficiency). This worked out fairly cleanly, but with D's abilit…
I'm curious of your opinion about AdaCore CCG, an SPARK to C compiler, for targets that don't have an Ada compiler yet.
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#46Earlier quoted context omitted.
To date, C's most proximate (management-friendly, technically similar, programmer-skill-adjacent) competition has been c++. (I'm trying to lay the groundwork for the next statement: I'm not claiming this is good, and excited to see Rust and friends as serious contenders. Stay with me.) Even in c++20, there are things you can do with macros you can't do in the language. This may change when reflection lands in c++2x,…
When I look at a lot of things that are done with reflection in C# I personally would much prefer the preprocessor and macros. Things that could be done in a type safe manner with a macro in a few lines become way more complex when using reflection.
Re: Macros on Steroids: How pure C can benefit from metaprogramming
#47Re: Macros on Steroids: How pure C can benefit from metaprogramming
#48Re: Macros on Steroids: How pure C can benefit from metaprogramming
#49Re: Macros on Steroids: How pure C can benefit from metaprogramming
#50Earlier quoted context omitted.
When I look at a lot of things that are done with reflection in C# I personally would much prefer the preprocessor and macros. Things that could be done in a type safe manner with a macro in a few lines become way more complex when using reflection.
Could I beg of you to give an example? Back when I was working in C#, there were quite a few times when I'd encounter things being done with reflection that I wish had been done with less code and a more type safe manner by just... not using reflection. I always just assumed a lot of that was just bad habits left over from the .NET 1.0 days, back before generics. So I'm curious if there's much overlap there? If it's…
The typical implementation is something like this:
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
NotifyPropertyChanged();
}
}
}
When you have 20 properties you have a ton of repetitive code. With a preprocessor I could reduce each property to a one liner. You can also do tricks with reflection but that's way more complex to implement.