top | item 46814737

(no title)

on_the_train | 1 month ago

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

discuss

order

Night_Thastus|1 month ago

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

on_the_train|1 month ago

It's still been the standard since c++11 and I've been using it every since in all teams I've worked in.

whobre|1 month ago

Same here

cpburns2009|1 month ago

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|1 month ago

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|1 month ago

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