Definitive way converting C source codes into C++

How do we, in the definitive way, convert C source codes into C++ having stdint.h, stdlib.h, string.h, stdio.h, inttypes.h, pthread.h, done mostly by regex find-replace?
Good luck with this valid C program.
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

struct class {
  int a[sizeof('a')];
} class;

int main()
{
  printf("%zd\n",sizeof(class));
  return 0;
}


See also http://david.tribble.com/text/cdiffs.htm

A simple mechanical conversion of C code to valid C++ code is both hard work, and the end result is just bad C++.
Last edited on
you simply rewrite the code.
anything else is like going to warp speed with no known course. aka. "warping into oblivion"
just compile the c code as-is into the project if that is possible. If the code works, use it, anything else is wasting time and time is money.

you can convert it to C++ 1990 procedural format with minimal work... change all the memory to new/delete, change printf to cout, and so on, a few basic changes and fix the header files and std:: namespace issues. If you want c++ 17 from C using OSO design, it is a total rewrite, there is NO way to convert it with just replace logic. There isnt much point in turning it into 1990s era C++. Why bother? You can go somewhere between these two extremes, procedural c++17 and swap arrays to vectors etc, but doing that with replacements is still a no-go.
Last edited on
If the C code compiles and works as wanted why change it?

As a learning experience, great idea.

But doing the conversion will require as much artistry and technical skill as creating an art masterpiece. Each converted program will be a work of art, hand crafted.
Topic archived. No new replies allowed.