News for hackers. Stuff that matters.
Re:New C features in GCC 13 by pmorici on Thursday May 04, 2023 @08:30AM (#35814244)
I don’t understand why anyone would use the “auto” variable type thing. In my experience it makes it impossible to read and understand code you aren’t familiar with.
Re:New C features in GCC 13 by unwind on Thursday May 04, 2023 @08:40AM (#35814335)
Well, the obvious (?) reason is to type less, and also reduce the risk of doing the wrong thing and using a type that is (subtly) wrong and having values converted which can lead to precision loss.

Also it can (in my opinion, brains seems to work differently) lower the cognitive load of a piece of code, by simply reducing the clutter.

Sure it can obscure the exact type of things, but I guess that's the trade-off some people are willing to do, at least sometimes.

Something like:

    const auto got = ftell(fp);
saves you from having to remember if ftell() returns int, long, long long, size_t, ssize_t, off_t or whatever and in many cases you can still use the value returned by e.g. comparing it to other values and so on without needing to know the exact type.

If you want to do I/O (print the number) then you have to know or convert to a known type of course.

This was just a quick response off the top of my head, I haven't actually used GCC 13/C2x yet although it would be dreamy to get a chance to port some old project over.

Re:New C features in GCC 13 by shrimp_emoji on Thursday May 04, 2023 @09:06AM (#35814650)
> you can still use the value returned by e.g. comparing it to other values and so on without needing to know the exact type.

No no no no nonononono. No!

Loose typing was a mistake. I think any sober analyst of C and C++ knows that. The languages have been trying to rectify it ever since.

But dynamic typing was an even bigger mistake. Perversely, it's one caused by a language not having a compiler that can type check the code, which C does.

I want to actually know what my code is doing, thanks. If you want "expressive" programs that are impossible to reason about, just build the whole thing in Python or JS. (And then pull the classic move of breaking out mypy or TypeScript half way in to development, tee hee.)

The only time `auto` is acceptable is when used for lambdas or things whose type is already deducable from the initializer, like `auto p = make_unique<Foo>()`.

Re:New C features in GCC 13 by unwind on Thursday May 04, 2023 @09:18AM (#35814804)
There is nothing "dynamic" about what I suggested. There is a real, concrete, static and compile-time known type at all times.

In this case it would be long. I fail to see the huge risk you're implying by operating upon a long-typed value without repeating the type name in the code.

    const auto pos_auto = ftell(fp);
    const long pos_long = ftell(fp);
I don't understand what you can do with 'pos_long' that would be dangerous doing with 'pos_auto' (again, disregarding I/O since then you typically have to know or cast).
Re:New C features in GCC 13 by shrimp_emoji on Thursday May 04, 2023 @08:03PM (#35823756)
> again, disregarding I/O since then you typically have to know or cast

Thank you for answering for me!

Re:New C features in GCC 13 by astrange on Thursday May 04, 2023 @09:54PM (#35824427)
`const long pos_long = ftell(fp);` contains a potential implicit case in the future if the return type of `ftell()` changes.

That's one reason type inference is safer than not inferring. Your program doesn't include semantics you didn't actually intend to be part of its meaning.

Also, I think lambdas would be annoying without it.

Re:New C features in GCC 13 by grumpyprole on Thursday May 04, 2023 @01:43PM (#35819321)
You are confusing dynamic types with type inference.
Re:New C features in GCC 13 by shrimp_emoji on Thursday May 04, 2023 @08:01PM (#35823746)
I'm not, although it apparently came off that way.

I meant that, to a person reading the code, `auto` tells you about as much about the type you're looking at as no type at all (like in a dynamically typed language).

This chatter said it better: https://news.ycombinator.com/item?id=35814337

Re:New C features in GCC 13 by grumpyprole on Friday May 05, 2023 @01:45AM (#35825616)
This is where tooling can help. An IDE could display the type next to all auto symbols if you want. Or allow you to query it with a keyboard shortcut. This gives the best of both worlds, rather than forcing everyone to write out the types all the time. Sometimes we simply don't care what the exact type is, e.g. if it's an object created and consumed by an API. We still want it type-checked, but we don't want to be forced to write out the type.
Re:New C features in GCC 13 by gradstudent on Thursday May 04, 2023 @09:38AM (#35815128)
typing less sounds like a minor benefit to me and the downsides are major: auto makes code unintelligible to humans on casual inspection

#noauto

Re:New C features in GCC 13 by dale_glass on Thursday May 04, 2023 @08:41AM (#35814351)
auto is at its best when you have something like:

    std::unordered_multimap<string, std::unordered_multimap<string, someclass>> getWidgets();
With templates you can easily have very unwieldy types, and there's not that much benefit from spelling them out explicitly.

Like any tool, there are good and bad uses of it. Well used, it removes unnecessary clutter and makes the code more readable.

Re:New C features in GCC 13 by myrmidon on Thursday May 04, 2023 @08:48AM (#35814426)
It becomes tolerable by using a text editor that is too clever for his own good and fills the typeinformation back in as hint.

But I'm not a big fan either.

Re:New C features in GCC 13 by HybridCurve on Thursday May 04, 2023 @08:55AM (#35814512)
I agree with you here. Many people might find it useful but this is something better suited for C++ which is full of nebulous typing features.
Re:New C features in GCC 13 by bonzini on Thursday May 04, 2023 @08:59AM (#35814560)
It makes sense to have it in macros. Though standard C still doesn't have statement expressions, so...
Re:New C features in GCC 13 by dzaima on Thursday May 04, 2023 @09:06AM (#35814642)
As an example of this, a generic `MAX` macro that doesn't evaluate its arguments multiple times, would be (using said GNU extension of statement expressions):

    #define MAX(A, B) ({ auto a = (A); auto b = (B); a>b? a : b; })
As-is, for such things I just use __auto_type, as it's already in the GNU-extension-land.
Re:New C features in GCC 13 by afdbcreid on Thursday May 04, 2023 @09:21AM (#35814851)
Do you really need to parenthesize the parameters? Is there something that can break the variable declaration into multiple statements?
Re:New C features in GCC 13 by dzaima on Thursday May 04, 2023 @09:25AM (#35814926)
Here, probably not (with proper arguments at least; without the parens something like `MAX(1;2, 3)` would compile without errors though), but I'm just used to doing it everywhere.
Re:New C features in GCC 13 by kps on Thursday May 04, 2023 @10:01AM (#35815465)
Here, no. It's just a habit or common style guideline to always parenthesize macro parameters since so many macros can otherwise break.
Re:New C features in GCC 13 by peterfirefly on Thursday May 04, 2023 @09:25AM (#35814925)
I wish ({...}) had been in C23.
Re:New C features in GCC 13 by uecker on Thursday May 04, 2023 @10:57AM (#35816395)
I agree, this is one of the more important common extensions we are still missing.
Re:New C features in GCC 13 by peterfirefly on Thursday May 04, 2023 @11:45AM (#35817241)
I'm still miffed it wasn't in C99 ;)
Re:New C features in GCC 13 by uecker on Thursday May 04, 2023 @12:19PM (#35817890)
Sorry, it was on my list of things to propose, but I did not have enough time (due to a job change). Others were more interested in lambdas.
Re:New C features in GCC 13 by peterfirefly on Thursday May 04, 2023 @12:30PM (#35818061)
You are forgiven. It must be like pushing water up a mountain. At least we got #embed, typeof, auto, constexpr, and sensible enums this time around.

How many of you guys have half of a C compiler lying around in a directory somewhere on your machines?

(And how do you find the time for WG14 AND writing code AND doing research? My cousin is in roughly the same field as you and you publish more than he does.)

Re:New C features in GCC 13 by uecker on Thursday May 04, 2023 @01:55PM (#35819502)
It is Sisyphean, but there also many synergies (I work on real-time computational imaging systems). And the specific things you mention were mostly not my work (I made typeof in GCC more coherent, which helped getting it standardized). But yes, I have a semi-complete C compiler around and many unfinished GCC branches...
Re:New C features in GCC 13 by dxuh on Thursday May 04, 2023 @09:05AM (#35814635)
There is this guideline of "Almost Always Auto" (https://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style...) and I have been following it for yeears both in my job and my personal projects and I have never been very confused by it or had any sort of bug because of it. I felt very reluctant about using it at all for quite a while myself, but in practice it just makes stuff easier and more obvious. A huge reason it's useful in C++ is generic code (stuff that depends on template parameters or has many template parameters) or deeply nested stuff (typing std::unordered_map<std::string, MyGreatObject::SomeValue>::iterator gets annoying), but it's nice almost everywhere. Most types are repeated and getting rid of those repetitions makes refactorings a lot easier and gets rid of some source of bugs. For example sometimes you forget to change all the relevant types from uint32_t to uint64_t when refactoring some value and stuff breaks weirdly (of course your compiler should warn on narrowing conversions, but just to illustrate the point, because it is very real).
Re:New C features in GCC 13 by pmarin on Thursday May 04, 2023 @09:48AM (#35815270)
>For example sometimes you forget to change all the relevant types from uint32_t to uint64_t when refactoring some value and stuff breaks weirdly

Use size_t

Re:New C features in GCC 13 by KerrAvon on Thursday May 04, 2023 @12:44PM (#35818335)
`size_t` may not be helpful. You can argue that some other specific typedef should have been used in this case, but it's kind of water under the bridge already.
Re:New C features in GCC 13 by tom_ on Thursday May 04, 2023 @01:38PM (#35819248)
size_t is for object sizes. It might not be 64 bits.
Re:New C features in GCC 13 by flohofwoe on Thursday May 04, 2023 @10:40AM (#35816103)
This gives a pretty good explanation why auto is useful in C:

https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu...

(TL;DR: it makes sense in macros)

Re:New C features in GCC 13 by Timpy on Thursday May 04, 2023 @11:24AM (#35816887)
Isn't auto already a keyword for specifying scope? I know it's never used and kind of redundant, but something like `auto x = foo(y);` is a terrible direction for C. Type being abundantly clear at all times is a huge feature.
Re:New C features in GCC 13 by tyler569 on Thursday May 04, 2023 @01:58PM (#35819533)
The accepted proposal does address and preserve `auto`'s use as a storage class, so `auto x = foo(y);` means type deduction based on the return type of `foo`, and `auto int x = 10;` declares an integer with automatic storage duration.