Automated conversion between C and C++ - Page 1
Forum section: electronics / general

author: GromBeestje
author_rank: Frequent Contributor
timestamp: March 05, 2025, 11:00:47 am
content: I am thinking about automated conversion between C and C++ codeSay, I have the following C codeCode:typedef struct {int a;int b;} example_t;void example_set_a(example_t* example, int val) { example->a=val; }void example_set_b(example_t* example, int val) { example->b=val; }int example_get_sum(example_t* example) { return example->a + example->b};The C++ equivalent would beCode:class cExample {private:int a;int b;public:void setA(int val);void setB(int val);int getSum();};void cExample::setA(int val) { a=val; }void cExample::setB(int val) { b=val; }int cExample::getSum() { return a + b; }The desired conversion should consist of: list all the members of the struct, convert naming conventionfrom snake case to camel case, and put them into the private section of a class, class name convertedaccordingly.Take all the functions that take a pointer to the struct as first parameter, and put them in the public sectionof the class. Inside the function, replace all the dereferences of the struct with nothing, such they refer tothe class instance.I know of sites likehttps://www.codeconvert.ai/c-to-c++-converterbut I don't want to use AI for somethingthat should be able to be done very mechanically, and I would like to run it locally.Are there any existing tools that could accomplish this job?If I were to create something myself, would yacc be worth investigating to parse the input?Edit: forgot part of the example

author: Nominal Animal
author_rank: Super Contributor
timestamp: March 05, 2025, 05:08:46 pm
content: Quote from: GromBeestje on March 05, 2025, 11:00:47 amWhy?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 anobject-orientedprogramming language. While you can so simple conversions from structures to classes, anything more complicated requires careful inspection of the C code anyway, with afunctional rewritein C++ more preferable to atranslation.Consider a common example, where a structure describes key-value pairs, sayCode: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 arrayaofnstructures can be sorted usingqsort(a, n, sizeof a, key_compare)orqsort(a, n, sizeof a, 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.

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 05, 2025, 05:40:56 pm
content: Translation is a lost cause.First you'd need to write a fully-compliant, ideally at least C11 to be reasonable, C analyzer. Then find "patterns" in the AST that your translator would identify and translate into idiomatic C++ (which is what GromBeestje seems to talk about).All this without error.And you'd still end up with sub-par C++ compared to carefully designed C++.

author: GromBeestje
author_rank: Frequent Contributor
timestamp: March 06, 2025, 07:45:06 pm
content: I'm not aiming for the fancy C++ stuff. I am looking for fairly simple stuff, as I described. Just copy-paste the code inside the functions, replacing the pointer.Not to get distracted by the details, but, my goal is embedded use, like, a driver to some sensor. Replacing get_temperature(some_sensor_t*) with SomeSensor::getTemperature(). stuff like that.