Remix.run Logo
whobre 6 hours ago

> auto main() -> int {

Dude…

cocoto 2 hours ago | parent | next [-]

In my opinion this syntax is super good, it allows to have all functions/method names starting at the same level, it’s way easier to read the code that way, huge readability improvement imo. Sadly nobody uses this and you still have the classic way so multiple ways to do the same thing…

vitaut 2 hours ago | parent [-]

This style is used in {fmt} and is great for documentation, especially on smaller screens: https://fmt.dev/12.0/api/#format_to_n

rovingeye 3 hours ago | parent | prev | next [-]

This has been valid C++ since C++ 11

direwolf20 24 minutes ago | parent [-]

It's unusual. Some, unusual, style guides require it. It's useful in some cases, even necessary in some which is why it was introduced, but not for simple "int"

sethops1 3 hours ago | parent | prev | next [-]

As someone who quit c++ over 15 years ago it's been comical to watch what this language has become.

webdevver 4 hours ago | parent | prev | next [-]

i was sincerely hoping i could get

    auto main(argc, argv) -> int
         int argc;
         char **argv;
to work, but alas it seems c++ threw pre-ansi argument type declarations out.
zabzonk 2 hours ago | parent [-]

> c++ threw pre-ansi argument type declarations out

they never were in C++.

CamperBob2 5 hours ago | parent | prev | next [-]

It's like calling a Ford Mustang Mach-E the "Model T++."

on_the_train 5 hours ago | parent | prev | next [-]

It's been the go-to syntax for 15 years now

Night_Thastus 5 hours ago | parent | next [-]

Go-to? I've never seen a project use it, I've only ever seen examples online.

whobre 4 hours ago | parent [-]

Same here

cpburns2009 4 hours ago | parent | prev [-]

Now I haven't touched C++ in probably 15 years but the definition of main() looks confused:

> auto main() -> int

Isn't that declaring the return type twice, once as auto and the other as int?

yunnpp 4 hours ago | parent | next [-]

No. The auto there is doing some lifting so that you can declare the type afterwards. The return type is only defined once.

There is, however, a return type auto-deduction in recent standards iirc, which is especially useful for lambdas.

https://en.cppreference.com/w/cpp/language/auto.html

auto f() -> int; // OK: f returns int

auto g() { return 0.0; } // OK since C++14: g returns double

auto h(); // OK since C++14: h’s return type will be deduced when it is defined

maccard 4 hours ago | parent [-]

What about

auto g() -> auto { return 0.0; }

yunnpp 2 hours ago | parent [-]

0.0 is a double, so I would assume the return type of g is deduced to be double, if that is what you're asking.

maccard 4 hours ago | parent | prev [-]

I really wish they had used func instead, it would have saved this confusion and allowed for “auto type deduction” to be a smaller more self contained feature

zabzonk 2 hours ago | parent [-]

the standard c++ committee is extremely resistant to introducing new keywords such as "func", so as not to break reams of existing code.

few 5 hours ago | parent | prev [-]

And their code example doesn't actually return a value!

Davidbrcz 5 hours ago | parent [-]

For main it's explicitly allowed by the standard, and no return is equal to return 0

direwolf20 23 minutes ago | parent | next [-]

which is super weird. If they can tell the compiler to allow no return, only for main, they can also tell it to pretend void return is int return of 0, only for main.

GrowingSideways 4 hours ago | parent | prev [-]

[dead]