5 ms·
I wish the C Standard Committe stopped smearing all C++ bullshit in to C. Now that many of the C++ people who promoted those features are abandoning the ship.
by pmarin 3y ago
I wish the C Standard Committe stopped smearing all C++ bullshit in to C. Now that many of the C++ people who promoted those features are abandoning the ship.
It's what you get when your C compilers are implemented in C++.
- pmorici 3y agoI 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.
- unwind 3y agoWell, 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.
- shrimp_emoji 3y ago> 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>()`.
- unwind 3y agoThere 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).
- shrimp_emoji 3y ago> again, disregarding I/O since then you typically have to know or cast Thank you for answering for me!
- astrange 3y ago`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.
- grumpyprole 3y agoYou are confusing dynamic types with type inference.
- shrimp_emoji 3y agoI'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 https://news.ycombinator.com/item?id=35814337
- 3y ago
- gradstudent 3y agotyping less sounds like a minor benefit to me and the downsides are major: auto makes code unintelligible to humans on casual inspection #noauto
- dale_glass 3y agoauto 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.
- myrmidon 3y agoIt 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.
- HybridCurve 3y agoI 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.
- bonzini 3y agoIt makes sense to have it in macros. Though standard C still doesn't have statement expressions, so...
- dzaima 3y agoAs 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.
- afdbcreid 3y agoDo you really need to parenthesize the parameters? Is there something that can break the variable declaration into multiple statements?
- dzaima 3y agoHere, 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.
- kps 3y agoHere, no. It's just a habit or common style guideline to always parenthesize macro parameters since so many macros can otherwise break.
- peterfirefly 3y agoI wish ({...}) had been in C23.
- uecker 3y agoI agree, this is one of the more important common extensions we are still missing.
- dxuh 3y agoThere is this guideline of "Almost Always Auto" (https://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-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).
- pmarin 3y ago>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
- flohofwoe 3y agoThis gives a pretty good explanation why auto is useful in C: https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu#n3006--n3007---type-inference-for-object-definitions https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu... (TL;DR: it makes sense in macros)
- Timpy 3y agoIsn'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.
- tyler569 3y agoThe 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.
- dale_glass 3y agoWhy "bullshit"? I looked at the article, and everything looks extremely reasonable, and desirable in C. * nullptr: fixes problems with eg, va_arg * better enums: Who doesn't want that? C is a systems language, dealing with stuff like file formats, no? So why shouldn't it be comfortable to define an enum of the right type? * constexpr is good, an improvement on the macro hell some projects have * unprototyped functions removed: FINALLY! That's a glaring source of security issues. Really I don't see what's there to complain about, all good stuff.
- oblio 3y ago> Really I don't see what's there to complain about, all good stuff. It's called "change" and people don't like it.
- hgs3 3y ago> nullptr: fixes problems with eg, va_arg nullptr is an overkill solution. The ambiguity could have been solved by mandating that NULL be defined as (void*)0 rather than giving implementations the choice of (void*)0 or 0.
- zajio1am 3y agoAre there any mainstream implementation where NULL is not typed as (void *)? That seems like a choice that would cause so many problems (type warnings, va_arg issues), i wonder why would anyone do that.
- peterfirefly 3y agoVintage code or code written by vintage coders. Code written by C++ programmers. Code written to be both C and C++.
- ori_b 3y agoNo.
- peterfirefly 3y ago
- myrmidon 3y agoThis is a completely unfair mischaracterization. A lot of these ARE relevant and useful improvements to the C language itself; constexpr reduces the need for macro-constants (which is nice), ability to specify the enum storage type is often helpful and clean keywords for static_assert etc. are a good idea too. And getting rid of the "void" for function arguments is basically the best thing since sliced bread.
- Kranar 3y ago> constexpr reduces the need for macro-constants const is sufficient to eliminate the use of macro-constants with the exception of the use of such constants by the preprocessor itself (in which case constexpr is also inapplicable).
- peterfirefly 3y ago#define DEF (ABC+(GHI<<JKL_SHIFT))
- Kranar 3y agoPlease make a point.
- peterfirefly 3y agoI did. This is exactly what we need for constexpr for.
- Kranar 3y agoNothing you've demonstrated requires the use of constexpr. A const is perfectly suited for that.
- peterfirefly 3y agoOne of us doesn't know C.