r/cpp • • Jul 29 '19

Is auto-conversion of C++ code to a simpler, modern, and not backwards-compatible version possible?

I know that this kind of speculation doesn't go well here but could an automatic conversion of C/C++ code to a new language that's pretty close to modern C++ but with fixes (e.g. initialization syntax) and the bad parts removed (e.g. implicit conversions) ever be possible? A conversion to Rust or D would be harder. If it's possible, we could have a language with lesser cognitive load, able to use most legacy libraries and with the good and familiar features of C++ left intact. The performance might be somewhat worse - e.g. because memory initialization after allocations is desired. However, such a language wouldn't require as much work as completely new languages because it could just copy new features from C++.

53 Upvotes

122 comments sorted by

View all comments

Show parent comments

5

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jul 29 '19

It's not for aesthetic purposes. Leaving variables uninitialised by accident leads to bugs. A more verbose syntax forces the user to opt into the more dangerous construct.

-2

u/SteveThe14th Jul 29 '19

To me this feels like having bad coding practices more than a requirement for a language change. If anything use a linter to catch your mistakes rather than make the language more verbose.

2

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jul 29 '19

"Why make the language safer when you could just download a third-party tool that lints your code?"

This argument is very weak. There's no reason for the language to be safer by default, and not everybody knows about linters and can use them.

0

u/SteveThe14th Jul 29 '19

Sure. It's a balance problem. I like writing short code, and writing int x = null; is just annoying and makes the code harder to quickly parse. I can see how for other people that's very convenient, but its a direction I don't really like C++ going in.

5

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jul 29 '19

Having a variable uninitialized and figuring out when it's set makes the code hard to parse. If anything, all variables should be const whenever possible. Having an uninitialized variable should be such a rare occurrence that having extra syntax for it would be completely justified.

-2

u/SteveThe14th Jul 29 '19

I just really disagree with this view of code and I don't enjoy code which has this ethos. It's one of the reasons I wish C++ could just break up already in the direction you prefer, and the direction I prefer.

4

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jul 29 '19

This "view" objectively increases safety. I don't understand how someone could disagree with this - please enlighten me.

1

u/SteveThe14th Jul 29 '19

I don't disagree it increases safety, it's just at the cost of verbosity. There's no need to be snappy.

4

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jul 29 '19

Didn't mean to sound snappy. What I find very weird is that you value terseness over safety. I would also argue that uninitialized variables (and non-const variables in general) harm readability, as the scope of the function now has more possibilities a reader/reviewer needs to consider.

Out of curiosity, what domain do you work in?

0

u/SteveThe14th Jul 29 '19

I'm in games and a tiny portion of my background is in C. So I suppose I just grew into the mentality of just being aware of things such as uninitialised values and raw pointers, and a lot of the safety features to me add a lot of extra typing for minimal impact. If I'd work in finance I'd probably want all the safety features I could get, and perhaps not use C++ at all.

Perhaps I'm just a dinosaur cowboy.

→ More replies (0)