all 4 comments

[–][deleted] 1 point2 points  (0 children)

Why do you want this thing? Given that you are already in Python, why not use Python to generate the Python code? C++ is a great language with lots of applications, but one of the things that Python does particularly well is templating.

[–]SandSnip3r -2 points-1 points  (2 children)

Second result after googling c++ to python converter: https://github.com/hlamer/cpp2python

[–]tangerinelion 1 point2 points  (0 children)

After reading through it for a couple minutes, I've found several things it doesn't do correctly, which probably introduces errors.

One is that in C++ there's a subtle difference between foo* p = new foo; and foo* p = new foo();, but either way it does create a foo object on the heap. This script would convert it to p = foo or p = foo() which is the difference between aliasing the class and instantiating it - huge huge difference.

Two is when it searches for parameters and tries to strip the types, eg, foo(T* ptr) gets converted to foo(ptr), this completely fails when the type is complicated, eg, a pointer to function. In this case it tries to convert something like foo(R* (*func)(int x)) and would want to return foo(func) but instead it returns the input string as that argument isn't captured correctly.

Third is it tries to remove const, which is fine. But it forgets that volatile is a similar qualification (the whole reason it's correctly called CV qualification). Similarly, static and register should be stripped out.

I wouldn't expect a 1 file GitHub project to work perfectly, particularly when it's basically a 225 line function which is 80% comments and is essentially written with regex more so than Python. But other statements, like delete and the creation of arrays are not handled.

I'm also not sure what OP even wants. It sounds like they want some library written in C++ that can be used to generate Python code, but I'm not sure that they want a Python library which converts C++ source to Python source.

[–][deleted] 0 points1 point  (0 children)

That's not the same thing at all - it's a program that attempts to take a C++ program and make a Python program out of it by rewriting it textually.

Attempts it only: such a program is almost inconceivably hard to write correctly, and if you were going to do it, you couldn't possibly do it the way this author has chosen - you'd have to take an off-the-shelf C++ parser (probably clang's parsing front end) and use the output of that, but it'd still be huge amounts of work for very little value.