I'm a college student and I have homework in C++.

My professor wants separate hpp and cpp files for each C++ class. And it just didn't feel good how much I had to type, and how much I had to click to create two new files for every class.

I haphazardly put together a simple Python script that would read a single file and generate all the needed classes (the easy stuff like class name, list of members, and list of things to #include I sprinkled with syntactic sugar, but the hard stuff to parse like typenames and function bodies I copied and pasted for the most part, except my script pokes around in a very primitive way to qualify method signatures with the class name when it generates the implementation file).

And I felt pretty good about it.

But I realized, that if a lot of other people had worked on a similar problem in the past, they probably could have done it better.

I know that Chicken scheme translates to C, and so does vala and genie, but from what I understand, with these sorts of translators, you don't really have full control over the C output.

Do you know of any translators that output C++ that largely keeps the semantics of C++ intact, so that the output C++ files look handwritten, but the source language is just loaded with wonderful syntax that makes life easier? Maybe even have the language mess with the body of functions so that if I typed something like vector<int> v = [1,2,3,4,5] it would become vector<int> v; v.push_back(1); v.push_back(2); .. in the generated source? Maybe have braces automatically inserted based on indentation? Have newlines serve as semicolons?

  • 2
    Typing is rarely the problem (esp. in C++). It's the retyping. – JeffO Nov 19 '13 at 2:36
  • 6
    No. Autogenerated code is always going to look ugly because it's trying to handle millions of cases in as little code as possible. I'd suggest just sucking it up and writing C++. Getting a good editor can ease the pain however by autogenerating stubs and headers – Daniel Gratzer Nov 19 '13 at 2:48
  • 5
    Several things to consider: You will not be learning the material you are intended to be learning. You may miss a point that the instructor is trying to teach and be critical for the next assignment. Your code will be obviously generated (lack of good comments, good variable, method names). It really isn't what you are trying to be taught. Follow the path and see where it gets you - don't try to skip to the end. – user40980 Nov 19 '13 at 2:56
  • 1
    @math4tots Not necessarily, most major syntactical transformations demand some form of gensymed variables. What's foo([1, 2, 3])?. – Daniel Gratzer Nov 19 '13 at 3:05
  • 6
    @math4tots This sounds like a job for editor-foo rather than "whole new compiler"-foo. Consider emacs, I have several macros defined to do boiler-platey things when writing C++. Additionally C++11 helps much of this. vector<int> foo = {1, 2, 3}. C++11 + Editor-Foo should work out great – Daniel Gratzer Nov 19 '13 at 3:13

No, a language like you are looking for is unlikely to exist.

I vaguely remember a few efforts to create new languages that tried to remove such warts out of C++ as header files, but invariably they are either compiled directly (no translation to C++) or the C++ they generate has a distinct machine-generated look and feel, because producing readable C++ wasn't (enough of) a requirement.

In most cases, the duplication that you get from having separate declarations and definitions is taken care of to a sufficient degree by advanced editor features, like macros, wizards and refactoring tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.