I am thinking about automated conversion between C and C++ code
Why?
No, seriously. Most C code is compilable as C++ without any modifications. The main difference between C and C++ is their approach to problem-solving, C++ being an
object-oriented programming language. While you can so simple conversions from structures to classes, anything more complicated requires careful inspection of the C code anyway, with a
functional rewrite in C++ more preferable to a
translation.
Consider a common example, where a structure describes key-value pairs, say
struct keyval {
int key;
const char *val;
};
int key_compare(const void *kv1, const void *kv2) {
const int key1 = ((const struct keyval *)kv1)->key;
const int key2 = ((const struct keyval *)kv2)->key;
return (key1 < key2) ? -1 : (key1 > key2) ? +1 : 0;
}
int val_compare(const void *kv1, const void *kv2) {
return strcoll(((const struct keyval *)kv1)->val, ((const struct keyval *)kv2)->val);
}
where an array
a of
n structures can be sorted using
qsort(a, n, sizeof a[0], key_compare) or
qsort(a, n, sizeof a[0], val_compare).
Now, consider what your translator will generate for this simple code, and compare to how you would implement the same functionality in C++. If the two are not the same, or at least very very similar, then how useful is your translator?
I've found for myself that translations and library-code reuse is much less useful than being able and willing to rewrite and adapt the code, even across languages. I often write test cases and implementation examples with all sorts of bells and whistles, so that when I write an initial adaptation, I don't need to work at multiple levels of complexity at once (both as the "user" of the interface, and as the "provider" of the interface); but later, when I have an actual use case, I rip out all the unneeded stuff, and what is left, can often be algorithmically optimized via simplification. When a result isn't needed or useful in existing code, why compute it at all?
For that reason, I'm very suspicious of code translators and code generators. I believe that the ability and willingness to rewrite existing code is absolutely necessary –– critical! –– in the long term for every software developer, and that those automated tools will only help avoid developing that skill, and degrade the long-term quality said developers produce. There is enough crappy software in the world already, in my opinion; let's not add to that, and try to do better instead.