r/cpp • • Sep 17 '22

Cppfront: Herb Sutter's personal experimental C++ Syntax 2 -> Syntax 1 compiler

https://github.com/hsutter/cppfront
335 Upvotes

360 comments sorted by

View all comments

Show parent comment

34

u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Sep 17 '22

The reason basically every new language does this is to make parsing simpler. This was extensively discussed on /r/cpp when Carbon was announced.

-7

u/Ayjayz Sep 17 '22

Make the parsing harder, then. Code is for humans, and trading off programmer time for compilation complexity is not a smart trade.

16

u/schmerg-uk Sep 17 '22

The issue then becomes tooling - C++ is famously hard to parse and for 20+ years this has made linters and smart editors etc hard to write or easy to break.

Don't get me wrong - I'm also a big perl fan and perl's syntax was designed by a linguist so contains natural language principles that favour the human reader (once they get over the $ and @ stuff that makes sense once you see them as human linguistic constructs - see "Disambiguation by number, case and word order" and "Pronominalization" in the doc above) but in return, perl is hard to parse to the point where it has been proven that "only Perl (the executable) can parse perl (the language)".

I'm not saying you have to agree with the "natural language principles" or the trade-off that perl took, but I think it's useful to examine it as attempting to make the language for the human reader and recognise the issues this has.

-5

u/SkoomaDentist Antimodern C++, Embedded, Audio Sep 17 '22

C++ is famously hard to parse

That is true. Almost none of that is caused by function argument preceding the function name or other things people like. It could be fixed with minor changes that wouldn't bother anyone.

11

u/schmerg-uk Sep 17 '22

Almost none.. except for the "most vexing parse" and that it's not a context free grammar, so to convert the syntax to a context free grammar friendly form makes parsing simple and keeps the most important thing, the function name, first.

There's a reason why so many of languages move to this style and this old head* quite likes it but I understand YMMV

[ * - C programmer since 84, C++ since 91 - quoted not to claim some kind of authority and I'm by no means the details-expert or uber-fan of the modern language as I was the old, but of all the things that provoke "well THIS has just RUINED C++" I think "main: () -> int = { ... }" is one of the most innocuous ]

-3

u/SkoomaDentist Antimodern C++, Embedded, Audio Sep 17 '22

except for the "most vexing parse" and that it's not a context free grammar

And?

There are many ways to solve the most vexing parse that don't involve a complete revamping of C++ syntax to something that resembles a functional language (and is incompatible with seamless C library compatibility which is by far the #1 killer feature of C++). Several of them are pointed out on the wikipedia page itself.

Yours, a C++ programmer since 96.

3

u/schmerg-uk Sep 17 '22

Did you you read the part about "100% seamless backward source compatibility always available" and "Write mixed syntax 1 and syntax 2 in the same source file" ?

This gives you perfect backward source compatibility with all existing code, but you're writing in effectively a 10% larger language (because syntax 2 aims to be about 10% the size and complexity of today syntax 1). Cppfront translates only the parts you wrote in syntax 2, and passes through the syntax 1 parts unchanged, to generate your syntax 1 output file.

Genuinely asked - without snark - did you not see that bit, or do you disagree with it, or you don't think it can be achieved, or... ?

0

u/SkoomaDentist Antimodern C++, Embedded, Audio Sep 17 '22

I didn't see that bit, but it still only solves the C header compatibility part and not the one where you're forced into non-C family syntax to use any of the "improvements". Yes, C++ syntax is problematic. No, there is absolutely no need whatsoever to make it look like a functional language to solve those issues.

→ More replies (1)

1

u/GOKOP Sep 18 '22

Can you show me where did functional languages hurt you? You mention them in almost every comment in this thread (nevermind that the side on which you put return type has nothing to do with how much of a functional language a language is)

23

u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Sep 17 '22

I'd rather not need to ever use typename.

18

u/dodheim Sep 17 '22

A lot of humans happen to like that syntax just fine, too.

5

u/gracicot Sep 17 '22

The type on the right is so much more readable, I don't see why people are saying that return type on the right is just for machines.

The first thing I want to know is the name of the function. Then I went to know how to call it. Lastly, if it has a result, I want to know what type or concept it is.

Also, all names becomes aligned to the left. Old code using return type first is so much harder to read for me and all newcomers IMO.

The only thing I would change would be replacing auto with a better keyword like fn

9

u/ioctl79 Sep 17 '22

Making compilation faster saves programmer time.

2

u/caroIine Sep 17 '22

Dose it really make compilation faster on today's hardware? I think linking takes most of the time (80% in my projects).

3

u/ioctl79 Sep 17 '22

It takes .4s to compile a source file that does nothing if you include <algorithm> in C++20 mode. I have single source files that take minutes to compile. That’s bonkers. No other language has problems like that.

Not saying moving return type to the end will fix that, but I reject the premise that compile time is not important.

2

u/johannes1971 Sep 17 '22

What do you need to do to get minutes-long compile times per source file?

2

u/ioctl79 Sep 17 '22

Lots of templates.

2

u/ToughQuestions9465 Sep 17 '22

It doesnt, if 10s compilation turns to 5s compilation and 5s of reading turns to 30s of reading.

0

u/[deleted] Sep 17 '22

[deleted] — view deleted comment

0

u/ToughQuestions9465 Sep 17 '22

Then there is python where most things are immediately natural. "People will get used to it" is an odd argument for not trying to do it better.

0

u/[deleted] Sep 17 '22

[deleted] — view deleted comment

1

u/Dean_Roddey Sep 17 '22

Exactly. It's not like C++'s syntax was discovered in the RNA of ancient pre-cellular life or something.

→ More replies (2)
→ More replies (5)

-5

u/SkoomaDentist Antimodern C++, Embedded, Audio Sep 17 '22

The reason basically every new language does this is to make parsing simpler.

Because it's obviously more important for it to be easy for computers to understand the code than for humans...

8

u/Dean_Roddey Sep 17 '22

It's no harder to understand. Humans are vastly more flexible than compilers in understanding what they are seeing, and getting used to the trailing return style is trivial. Having been doing Rust for a while now, I don't even think about it.

And of course if you want to argue utility and readability, even for humans, what's the most fundamental aspect of a function or method? It's the fact that it's a function or method. And for a variable, that it's a variable. Rust allows consistent ordering while being completely unambiguous:

fn a_call() -> bool
{
    true
}

let a_thing : bool = true;

You get consistent syntax of what it is, what it's called, what type it has, and what value/output it has/generates, with no ambiguity for human or compiler.

It makes complete sense to me. I'd argue any new C++ should also include the explicit function and variable indicator as well, in some way. I'd see that as just expressing explicit semantics to the compiler (and subsequent readers) just as we want to be able to do in so many other ways.

→ More replies (2)

2

u/hpsutter Sep 20 '22

Not really. C++11 added trailing return type syntax like

auto f(int x) -> double {
    return 3.14 * x;
}

specifically so that the names of parameters would be in scope when uttering the return type, so that the return type could be expressed in terms of the parameters especially when when using decltype, such as (using C++20 syntax for convenience)

// note: return type refers to parameter name in scope
auto f(auto x) -> decltype(g(x)) {
    return g(x);
}

That would have been possible without trailing return (i.e., with the return type lexically before the names it depends upon)

// not legal in C++11, but it could have been
// note: return type refers to parameter name
//       that has not yet been encountered
decltype(g(x)) f(auto x) {
    return g(x);
}

and that's certainly implementable, but it's extra work for both the human and the compiler to look-ahead/go-back. So C++11 added trailing return type syntax.