4 ms·
> auto main() -> int { Dude…
by whobre 9mo ago
> auto main() -> int {
Dude…
- on_the_train 9mo agoIt's been the go-to syntax for 15 years now
- Night_Thastus 9mo agoGo-to? I've never seen a project use it, I've only ever seen examples online.
- whobre 9mo agoSame here
- on_the_train 9mo agoIt's still been the standard since c++11 and I've been using it every since in all teams I've worked in.
- cpburns2009 9mo agoNow 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 9mo agoNo. 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 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 9mo agoI 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 9mo agothe standard c++ committee is extremely resistant to introducing new keywords such as "func", so as not to break reams of existing code.
- maccard 9mo agoIndeed. I am a frequent critic of the c++ committee’s direction and decisions. There’s no direction other than “new stuff” and that new stuff pretty much has to be in the library otherwise it will require changes that may break existing code. That’s fine. But on the flip side, there’s a theme of ignoring the actual state of the world to achieve the theoretical goals of the proposal when it suits. Modules are a perfect example of this - when I started programming professionally modules were the solution to compile times and to symbol visibility. Now that they’re here they are neither. But we got modules on part. The version that was standardised refused to accept the existence of the toolchain and build tools that exist, and as such refused to place any constraints that may make implementation viable or easier. St the same time we can’t standardise Pragma once because some compiler may treat network shares or symlinks differently. There’s a clear indication that the committee don’t want to address this, epochs are a solution that has been rejected. It’s clear the only real plan is shove awkward functional features into libraries using operator overloads - just like we all gave out to QT for doing 30 years ago. But at least it’s standardised this time?
- CamperBob2 9mo agoIt's like calling a Ford Mustang Mach-E the "Model T++."
- few 9mo agoAnd their code example doesn't actually return a value!
- Davidbrcz 9mo agoFor main it's explicitly allowed by the standard, and no return is equal to return 0
- GrowingSideways 9mo ago[dead]
- direwolf20 9mo agowhich 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.
- webdevver 9mo agoi 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 9mo ago> c++ threw pre-ansi argument type declarations out they never were in C++.
- sethops1 9mo agoAs someone who quit c++ over 15 years ago it's been comical to watch what this language has become.
- rovingeye 9mo agoThis has been valid C++ since C++ 11
- direwolf20 9mo agoIt'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"
- rovingeye 9mo agoit's literally the exact same thing. We use trailing return types to be consistent across the language.
- direwolf20 9mo agoWe use trigraphs to be consistent across the language.
- cocoto 9mo agoIn 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 9mo agoThis style is used in {fmt} and is great for documentation, especially on smaller screens: https://fmt.dev/12.0/api/#format_to_n https://fmt.dev/12.0/api/#format_to_n