News for hackers. Stuff that matters.
Re:New C features in GCC 13 by frde on Thursday May 04, 2023 @08:04AM (#35814049)
I don't write C / C++ so I'm not too aware of what's going on there, but wouldn't someone that wants those features just switch to C++? Is there any reason to change C at this point?
Re:New C features in GCC 13 by Someone on Thursday May 04, 2023 @08:33AM (#35814273)
Switching to C++ gets you lots of things that aren’t “C-like” (that, of course, is a vague term that, as this thread shows, people will disagree about, but I think there’s consensus that C++ has many features that aren’t C-like), and may get you subtle bugs because of small incompatibilities between C and C++. For example, sizeof('x') is 1 in C++, but >1 in C because 'x' is a char in C++ and an int in C.

https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

Re:New C features in GCC 13 by nequo on Thursday May 04, 2023 @11:08AM (#35816573)
Why is

  sizeof('x')
equal to 4 if

  char letter = 'x';

  sizeof(letter)
is equal to 1, just like `sizeof(char)`? If `'x'` is represented as an `int` in C, shouldn't `letter` in this example also be represented as an `int`?
Re:New C features in GCC 13 by tialaramex on Thursday May 04, 2023 @11:32AM (#35817020)
No. The type of 'x' is int. It so happens on your platform (and most available systems today) sizeof(int) == 4.

The type of letter was explicitly char, and sizeof(char) == 1 by definition in C.

char letter = 'x'; is a type coercion. That literal is an integer, with the value 120 and then it's coerced to fit in the char type, which is an 8-bit integer of some sort (might be signed, might not, doesn't matter in this case).

Re:New C features in GCC 13 by nequo on Thursday May 04, 2023 @11:37AM (#35817119)
Does this mean that `word` in

  char *word = "xyz";
is a pointer to an array of four `int`s, `'x'`, `'y'`, `'z'`, and `'\0'`? When I evaluate

  sizeof(*word)
I do get 1 instead of 4, even though `*word` is pointing to `'x'`. Where are the remaining 3 bytes in memory?
Re:New C features in GCC 13 by lucozade on Thursday May 04, 2023 @11:45AM (#35817246)
If you specifically type it as char * the it's a pointer to chars each of which has size 1.
Re:New C features in GCC 13 by seba_dos1 on Thursday May 04, 2023 @11:47AM (#35817287)
Not at all. There are no character literals in "xyz", this is a string literal and it's unrelated to what your parent was saying.
Re:New C features in GCC 13 by jcranmer on Thursday May 04, 2023 @01:23PM (#35818993)
A char is 1 byte by definition. But the type of a character literal (the 'x' syntax) is not a char, but an int instead.

The C type system generally matters so little that the type of an expression has little relevance (sizeof is the most notable exception to that rule), which obscures this fact.

Re:New C features in GCC 13 by kzrdude on Thursday May 04, 2023 @02:03PM (#35819616)
you'll have to understand the 'x' syntax and the "xyz" syntax as two different things. Different quotes.
Re:New C features in GCC 13 by nequo on Thursday May 04, 2023 @02:58PM (#35820408)
I know. But my understanding was that `"xyz"` is an array of characters so that these two would have the same representation in memory:

  char word[] = {'x', 'y', 'z', '\0'};  // sizeof(word) = 4, sizeof(*word) = 1
  char word[] = "xyz";                  // sizeof(word) = 4, sizeof(*word) = 1
What I did not realize was that the above two are not the same as this:

  char *word = "xyz";  // sizeof(word) = 8, sizeof(*word) = 1
Re:New C features in GCC 13 by kzrdude on Thursday May 04, 2023 @03:03PM (#35820508)
What I see there is that you seem to highlight the difference between using sizeof with an array and sizeof with a pointer, which makes a difference, even if array-decays-to-pointer is a rule in most other contexts.
Re:New C features in GCC 13 by nequo on Thursday May 04, 2023 @03:11PM (#35820623)
Right, I am mixing up two things here. You are right that bringing up pointers here is a mistake.

But apart from that, I would expect `{'x', 'y', 'z', '\0'}` to have size 16 rather than size 4 because it consists of four character literals which each have size 4 on my machine.

Re:New C features in GCC 13 by kzrdude on Thursday May 04, 2023 @03:38PM (#35821002)
Maybe do not overthink it. 'x' is called a character literal, but it has the type int.

`{'x', 'y', 'z', '\0'}` does not have a type by itself at all, but it's valid syntax to use it to initialize various structs and arrays - some of those will have the size you are looking for, depending on which type of array or struct you choose to initialize with that.

These following two are incidentally valid whole expressions and have well defined types of char[4] and int[4] respectively. They have different sizes, as is expected from their types.

    (char[]){'x', 'y', 'z', '\0'}

    (int[]){'x', 'y', 'z', '\0'}
(This syntax is called compound literal and can be used with structs and arrays.)
Re:New C features in GCC 13 by nequo on Thursday May 04, 2023 @04:33PM (#35821686)
Thank you for the explanation and the Godbolt example! I appreciate it. Apologies for fumbling around in confusion.
Re:New C features in GCC 13 by peterfirefly on Thursday May 04, 2023 @03:46PM (#35821109)
sizeof() returns the number of "units" that something -- an expression or a type -- takes up. What do you think those units are?

They are literally defined as "characters". sizeof(char) is always 1.

Your confusion (besides the pointer thing) is that 'x' is a funny way to write an int, not a char.

Re:New C features in GCC 13 by nequo on Thursday May 04, 2023 @04:27PM (#35821618)
It seems to me that `sizeof` returns the number of bytes that the thing takes up in memory. For example:

  int numbers[] = {1, 2, 3};  // sizeof(numbers) = 12
> Your confusion (besides the pointer thing) is that 'x' is a funny way to write an int, not a char.

Yes, this might be it. So the way to get a `char` value that contains "c" is to use type coercion and write it as `(char) 'c'`. This changes the representation in memory so that it now takes up only one byte rather than four, right?

Re:New C features in GCC 13 by _kst_ on Thursday May 04, 2023 @09:11PM (#35824181)
`(char)'c'` is an expression of type char.

Its size is one byte -- but the size of an expression isn't really relevant, since it's (conceptually) not stored in memory.

You can assign the value of an expression to an object, and that object's size depends on its declared type, not on the value assigned to it. The cast is very probably not necessary.

    char c1 = 'c'; // The object is one byte; 'c' is converted from int to char
    int  c2 = 'c'; // The object is typically 4 bytes (sizeof (int))
The fact that character constants are of type int is admittedly confusing -- but given the number of contexts in which implicit conversions are applied, it rarely matters. If you assign the value 'c' to an object of type char, there is conceptually an implicit conversion from int to char, but the generated code is likely to just use a 1-byte move operation.
Re:New C features in GCC 13 by _kst_ on Thursday May 04, 2023 @09:05PM (#35824144)
The representation of an object is determined by how the object itself is defined.

An initializer doesn't change that. It only affects the value stored in the object when it's created.

A special case exception is that an array object defined with empty square brackets gets its length from the initializer, so

    char word[] = "xyz";
is a shorthand for, and is exactly equivalent to:

    char word[4] = "xyz";
Re:New C features in GCC 13 by _kst_ on Thursday May 04, 2023 @09:02PM (#35824127)
word is of type char*, a pointer to a (single) object of type char.

The initializer means that the char object it points to happens to be the first (0th) element of an array containing 4 elements with values 'x', 'y', 'z', and '\0'.

Most manipulation of arrays in C is done via pointers to the individual elements, and arithmetic on those pointers. (Incrementing a pointer value yields a pointer to the next element in the array.)

For example, `sizeof word` gives you the size of the pointer object, but `strlen(word)` yields 3, because it calls a library function that performs pointer arithmetic to find the trailing '\0' that marks the end of the string. (A "string" in C is a data layout, not a data type.)

Re:New C features in GCC 13 by gallier2 on Friday May 05, 2023 @06:39AM (#35827491)
People often forget that 'ab' or '1ert' multi-char immediate are allowed in C. They are almost unusable as they are highly un-portable (because of endianess issues between the front-end and the back-end).
Re:New C features in GCC 13 by tialaramex on Friday May 05, 2023 @10:47AM (#35829913)
This is once in a while kinda useful, aside from the data layout issue for stuff like a FourCC.

Rust has e.g. u32.to_le_bytes() to turn an integer into some (little endian) bytes, but I don't know if there's a trivial way to write the opposite and turn b"1ert" (which is an array of four bytes) into a native integer.

Edited to Add: oh yeah, it has u32.from_le_bytes(b"1ert"). I should have checked.

Re:New C features in GCC 13 by eMSF on Thursday May 04, 2023 @11:30AM (#35817002)
The type of the expression 'x' is int, not char (in C). The type of an expression consisting of a variable name is the type of the variable (as far as sizeof is concerned).
Re:New C features in GCC 13 by _kst_ on Thursday May 04, 2023 @08:23PM (#35823895)
In the declaration

    char letter = 'x';
the initialization expression 'x', which for historical reasons is of type int in C, is implicitly converted to the type of the object. `letter` is a `char` because you defined it that way.

If you had written

    int letter = 'x';
that would be perfectly valid, and the conversion would be trivial (int to int).

It's just like:

    double x = 42;
`sizeof 42` might be 4 (sizeof (int)), but `sizeof x` will be the same as `sizeof (double)` (perhaps 8).
Re:New C features in GCC 13 by pmarin on Thursday May 04, 2023 @08:18AM (#35814149)
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++.

Re:New C features in GCC 13 by dale_glass on Thursday May 04, 2023 @08:36AM (#35814301)
Why "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.

Re:New C features in GCC 13 by oblio on Thursday May 04, 2023 @09:03AM (#35814612)
> Really I don't see what's there to complain about, all good stuff.

It's called "change" and people don't like it.

Re:New C features in GCC 13 by hgs3 on Thursday May 04, 2023 @09:04AM (#35814624)
> 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.

Re:New C features in GCC 13 by zajio1am on Thursday May 04, 2023 @09:14AM (#35814754)
Are 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.
Re:New C features in GCC 13 by peterfirefly on Thursday May 04, 2023 @09:19AM (#35814819)
Vintage code or code written by vintage coders.

Code written by C++ programmers.

Code written to be both C and C++.

Re:New C features in GCC 13 by ori_b on Thursday May 04, 2023 @10:08AM (#35815572)
No.
Re:New C features in GCC 13 by peterfirefly on Thursday May 04, 2023 @09:16AM (#35814780)
That would have been my preference as well. Either force it to be (void*)0 or, maybe, allow it to be 0 iff it has the same size and parameter passing method.
Re:New C features in GCC 13 by cremno on Thursday May 04, 2023 @10:00AM (#35815450)
dmr would've approved of going a step further - only nullptr, no 0 and (void*)0:

>Although it would have been a bit of a pain to adapt,

>an '89 or '99 standard in which the only source representation

>of the null pointer was NULL or nil or some other built-in token

>would have had my approval.

https://groups.google.com/g/comp.std.c/c/fh4xKnWOQuo/m/IAaOe...

Re:New C features in GCC 13 by peterfirefly on Thursday May 04, 2023 @09:05AM (#35814633)
constexpr is also ridiculously simple to implement -- because the existing compilers already do something similar internally for all enumeration constants.

(Enumeration constants are the identifiers defined inside enum xxx {...})

Re:New C features in GCC 13 by flohofwoe on Thursday May 04, 2023 @10:37AM (#35816063)
...and most compilers also already silently treat compile time constant expressions like constexpr, an explicit constexpr just throws an error if the expression isn't actually a compile time constant.
Re:New C features in GCC 13 by kats on Thursday May 04, 2023 @10:19AM (#35815747)
It has a lot of costs to add to the C language, even if it's just the increased complexity in the documentation, and doesn't effect c99. Every processor, OS, programming language needs used in business needs to fully support a C standard. So adding to C effects every processor and computer architecture, every new OS, every new language.

If you look at CPPreference you can see how much complexity has been added to the C standard in the last few years.

Re:New C features in GCC 13 by dale_glass on Thursday May 04, 2023 @02:25PM (#35819936)
What do those have to do with that? A processor has no need to know anything about constexpr, auto, or static_assert.

In fact I don't see anything that needs support anywhere but the actual compiler.

Re:New C features in GCC 13 by quelsolaar on Thursday May 04, 2023 @03:43PM (#35821065)
constexpr is terrible.

-constexpr is not anything like constexpr in C++. -It makes no guarantees about anything being compile time. -It in no way reflects the ability of the compiler to make something compile time. -It adds implementation burden by forcing the implementations to issue errors that do not reflect any real information. (For instance you may get an error saying your constexpr isnt a constant expression, but if you remove the constexpr qualifier, then the compiler can happily solve it as a constant expression) -All kinds of floating point issues.

We should not have admitted this in to the standard, please do not use.

nullptr is the third definition of null. one should be enough, two was bad. why three?

Re:New C features in GCC 13 by dale_glass on Thursday May 04, 2023 @04:19PM (#35821518)
Well, that's interesting.

Got any more information on that? Why does it fail in that way? Is that an implementation or a specification problem?

Re:New C features in GCC 13 by quelsolaar on Thursday May 04, 2023 @04:46PM (#35821816)
Im in the WG14 so ive been involved in the discussions.

It fails for 2 reasons:

In order to make it easy to implement it had to be made so limited, that it in no way useful.

The second reason, and the real killer, is the "as if" rule. It states that any implementation can do what ever it wants, as long as the output of the application is the same. This means that how a compiler goes about implementing something is entirely up to the compiler. This means that any expression can be compile or execution time. You can even run the pre-processor at run time if you like! This enables all kinds of optimizations.

In reality, modern compilers like gcc, llvm and MSVC are far better at optimizing than what constexpr permits. However since the specification specifies exactly what can be in a constexpr, the implementations are required to issue an error if a constexpr does something beyond this.

Re:New C features in GCC 13 by dale_glass on Thursday May 04, 2023 @06:43PM (#35823099)
Okay, so that's a good start, but I still don't get it.

> In order to make it easy to implement it had to be made so limited, that it in no way useful.

Such as?

> The second reason, and the real killer, is the "as if" rule.

Why is that a problem? It sounds like a benefit. It means that the optimization can't break anything, which to me is kind of the point.

Re:New C features in GCC 13 by quelsolaar on Friday May 05, 2023 @05:28AM (#35827005)
> Such as?

Loops, function calls.... things available in C++

>Why is that a problem? It sounds like a benefit. It means that the optimization can't break anything, which to me is kind of the point

As-if is great! but the problem is that constexpr tricks people in to thinking that something is done at compile time, and the as-if rule overrides that and lets the implementation do it when ever it wants. constexpr is a feature over ridden by a fundamental rule of the language.

Re:New C features in GCC 13 by flohofwoe on Friday May 05, 2023 @03:21AM (#35826270)
"Modern compilers" still fail at:

    const int bla = 23;
    const int blub[bla] = { 0 };
(see: https://www.godbolt.org/z/hjessMhGK)

Isn't this exactly what constexpr is supposed to solve?

Re:New C features in GCC 13 by quelsolaar on Friday May 05, 2023 @05:29AM (#35827008)
If so why not fix const? Why add a whole new keyword? Why complicate the language?
Re:New C features in GCC 13 by flohofwoe on Friday May 05, 2023 @07:10AM (#35827717)
Don't know, you're in WG14, not me :D

Maybe 'const' can't be fixed without breaking existing source code?

I don't really have a problem with adding a new keyword for 'actually const', maybe calling it 'constexpr' means C++ people have wrong expectations though, dunno.

For me, having constexpr being explicit about only accepting actual constant expressions, and creating an error otherwise (instead of silently falling back to a 'runtime expression') is fine, but the existing keyword 'const' has an entirely different meaning, there's just an unfortunate naming collision with other languages where const means 'compile time constant'.

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.
Re:New C features in GCC 13 by myrmidon on Thursday May 04, 2023 @08:43AM (#35814371)
This 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.

Re:New C features in GCC 13 by Kranar on Thursday May 04, 2023 @12:32PM (#35818101)
> 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).

Re:New C features in GCC 13 by peterfirefly on Thursday May 04, 2023 @12:48PM (#35818401)

    #define DEF   (ABC+(GHI<<JKL_SHIFT))
Re:New C features in GCC 13 by Kranar on Thursday May 04, 2023 @05:00PM (#35822009)
Please make a point.
Re:New C features in GCC 13 by peterfirefly on Thursday May 04, 2023 @05:42PM (#35822493)
I did. This is exactly what we need for constexpr for.
Re:New C features in GCC 13 by Kranar on Thursday May 04, 2023 @09:46PM (#35824379)
Nothing you've demonstrated requires the use of constexpr. A const is perfectly suited for that.
Re:New C features in GCC 13 by peterfirefly on Friday May 05, 2023 @02:29AM (#35825863)
One of us doesn't know C.
Re:New C features in GCC 13 by Kranar on Friday May 05, 2023 @11:36AM (#35830628)
https://godbolt.org/z/de1YGG5r1

I hope for your sake that you don't speak this way other than anonymously on the Internet; it reflects very poorly of you.

Re:New C features in GCC 13 by hbossy on Thursday May 04, 2023 @08:07AM (#35814069)
C and C++ are separate languages and lots of people don't like C++.
Re:New C features in GCC 13 by lou1306 on Thursday May 04, 2023 @08:24AM (#35814190)
Maybe. But by reading the article one does get the impression that GCC devs (and C2X proposal authors) really like C++: the language is mentioned 16 times, and easily half of the features are lifted more or less as-is from there.
Re:New C features in GCC 13 by seba_dos1 on Thursday May 04, 2023 @09:17AM (#35814796)
As a C programmer, it just seems like they finally took the minority of ideas added by C++ that were actually good ideas and added them back to C. Aside of `auto` which I'm ambivalent about (I think the only place where it's useful are macros) those all make perfect sense in context of C and I believe the only reason for C++ having them first is that C++ simply evolved faster.
Re:New C features in GCC 13 by klodolph on Thursday May 04, 2023 @09:47AM (#35815256)
It makes a lot of sense for the languages to be harmonized with each other. Differences like noreturn versus [[noreturn]] does nobody any favors. C++ has all these wacky things you can do with constexpr functions, and C is getting a VERY LIMITED version of this that only applies to constants, addressing a long-standing deficiency, where C provides only a way to define named constants as int type (using enum) or using macros, and you really want to be able to define constants as any type you like. The "const" qualifier doesn't do that, you see... it really means a couple different things, but the main one is "read-only", which is not the same as "constant".
Re:New C features in GCC 13 by steveklabnik on Thursday May 04, 2023 @01:22PM (#35818962)
One of the benefits, historically, to both languages is that they share a very large chunk of the language in common. It's therefore in their common interest to try and maintain that common subset wherever possible. The goal here (just to be clear, from my outside perspective) isn't to unify the languages, it's to ensure that stuff that's the same stays roughly the same. If the same code produces two different things, based on the language, that's unfortunate. Code that works in one but doesn't compile in the other is totally fine, of course.
Re:New C features in GCC 13 by frde on Thursday May 04, 2023 @08:11AM (#35814098)
I guess my question is: If you want `auto`, why put it in C instead of using C++ with no other C++ specific feature besides auto?

I get people don't like classes / templates / .. but there isn't any reason one has to use those.

Re:New C features in GCC 13 by masklinn on Thursday May 04, 2023 @08:16AM (#35814134)
> I guess my question is: If you want `auto`, why put it in C instead of using C++ with no other C++ specific feature besides auto?

Because they're orthogonal, and making function bodies less verbose with no loss of expressivity is nice, without needing to significantly alter the language?

Pretty much every C competitor has local type inference, and C actually needs more than most due to the `struct` namespace, typing `struct Foo` everywhere is annoying, and needing to typedef everything to avoid it is ugly.

Also C++ is missing convenient C features, like not needing to cast `void*` pointers, and designated initialisers were only added in C++20.

Re:New C features in GCC 13 by pmarin on Thursday May 04, 2023 @08:40AM (#35814337)
Type inference only make the code harder to read. You ended doing mental compiler work when you could just write the damm type.

And the people who say "I Just hover the variable in my IDE" It doesn't work in a terminal with grep, you can't hoved a diff file and not even github do the hover thing.

Combine that with the implicit type promotio rules of C. Have Fun.

Re:New C features in GCC 13 by masklinn on Thursday May 04, 2023 @08:44AM (#35814382)
> Type inference only make the code harder to read.

Nonsense.

> Combine that with the implicit type promotio rules of C. Have Fun.

This sort of trivial TI does not make that any worse. C is broken, it neither breaks nor unbreaks C.

Re:New C features in GCC 13 by hgs3 on Thursday May 04, 2023 @09:12AM (#35814727)
typeof and auto are useful for writing type-generic macros.
Re:New C features in GCC 13 by pmarin on Thursday May 04, 2023 @10:55AM (#35816343)
Yeah but looking how auto has been abused in C++ I don't think it worth it.
Re:New C features in GCC 13 by peterfirefly on Thursday May 04, 2023 @11:13AM (#35816658)
C23's auto is not nearly as magic as C++'s auto. You can use it for the simple things but not for the perversely creative abuse scenarios you fear.
Re:New C features in GCC 13 by circuit10 on Thursday May 04, 2023 @08:16AM (#35814139)
I guess one possible reason is if there’s no C++ compiler for an obscure platform as it would be too much work, but there is an up-to-date C compiler
Re:New C features in GCC 13 by klodolph on Thursday May 04, 2023 @09:55AM (#35815373)
Yeah, seems like half of the embedded architectures are like this. Well, the C compiler is not quite standards compliant, and often made to an older version of the C spec, but give it time—the C2x standard comes out this year, and it may not benefit people in the embedded space until some years down the road.

Second-best time to plant a tree, and all.

Re:New C features in GCC 13 by klodolph on Thursday May 04, 2023 @09:52AM (#35815338)
You end up having to turn a lot of C++ features off in order to get the experience you want in certain environments. In an application running on a modern Windows/Linux/Mac system, it’s no big deal to use those features.

Some platforms also just don’t have C++ compilers. Yes, they still exist. You buy some microcontroller, download an IDE from the manufacturer's web site, and you get some version of C with a couple extensions to it. And then there are all the random incompatibilities between C and C++, where C code doesn’t compile as C++, or gives you a different result.

Re:New C features in GCC 13 by jcelerier on Thursday May 04, 2023 @10:12AM (#35815630)
What's "a lot of features" ? -fno-rtti, -fno-exceptions?

> Some platforms also just don’t have C++ compilers

It's not like they're going to have C23 compilers either

Re:New C features in GCC 13 by flohofwoe on Thursday May 04, 2023 @10:33AM (#35815992)
> It's not like they're going to have C23 compilers either

Niche compilers like SDCC (https://sdcc.sourceforge.net/) are actually keeping track of recent C language improvements quite well.

Re:New C features in GCC 13 by klodolph on Thursday May 04, 2023 @10:24AM (#35815843)
C++ has a lot of funny rules when it comes to constructors and initializers. It's easy to accidentally to do something unintended, and end up with code that relies on initialization order.
Re:New C features in GCC 13 by ori_b on Thursday May 04, 2023 @10:07AM (#35815549)
Yes, so why copy paste C++ into C?
Re:New C features in GCC 13 by rwmj on Thursday May 04, 2023 @08:18AM (#35814151)
C++ drags in a ton of other baggage that a large enough number of programmers don't want.
Re:New C features in GCC 13 by trelane on Thursday May 04, 2023 @08:52AM (#35814483)
Especially when there's Go, Rust, etc. these days. There is so much legacy with C++ the language that it's pretty easy do do stuff subtly wrong if you're not rigidly careful and adhering to a style guide that forces you to only use the safer bits.
Re:New C features in GCC 13 by flohofwoe on Thursday May 04, 2023 @10:16AM (#35815696)
Many things that C++ added on top of C aren't actually improvements (I guess the most charitable thing that can be said about C++ is that it identifies and weeds out all the stupid ideas before they can make it into C).
Re:New C features in GCC 13 by pjmlp on Thursday May 04, 2023 @01:57PM (#35819520)
At least WG21 actually acknowledges solving security issues and UB problems are a real problem that needs to be sorted out.

Meanwhile, WG14, "not our problem ".

Re:New C features in GCC 13 by shrimp_emoji on Thursday May 04, 2023 @08:12PM (#35823823)
Trying to figure out whether it's well-defined to compare two pointers to different objects in memory...

...By reading the C++ standard: "it's well-defined"

...By reading the C standard: *self-referencing Zalgo text quoted by dozens of StackOverflow thread debates where no completely confident conclusion is ever reached, although 3/4 people way smarter than you think it's well-defined and blame observations to the contrary on compiler bugs which they've reported to all the major compiler maintainers with varying reception from said maintainers as to whether they agree that those are actually bugs, forcing you, at the end of the day, to realize that you should really be writing your compiler's C, not aspiring to a universal, platonic ideal of C*

Re:New C features in GCC 13 by uecker on Saturday May 06, 2023 @10:02AM (#35841485)
Why? I do not find the wording in the C standard less clear than the C++ wording (where the result is unspecified for unrelated pointers).
Re:New C features in GCC 13 by ho_schi on Thursday May 04, 2023 @02:54PM (#35820367)
A usual programmer doesn’t need most features of C++ but there are many important:

Generic-programming with templates, a usable std::string, smart-pointers, references, the howl standard-library (streams, file access, containers, threads).

The controversial ones seem to be exceptions and classes. Exceptions affect programming flow, exception safety is very hard and the runtime costs are an issue depending on the environment. Class and inheritance are complicated feature, operator overloading is one of the best stuff I’ve seen. But I can understand why many programmers don’t want handle all the special rules involving classes.

Re:New C features in GCC 13 by flohofwoe on Friday May 05, 2023 @03:14AM (#35826238)
This would add a lot of complexity into the C compiler and stdlib, and C would just end up as another C++. IMHO it is still important that a C compiler and stdlib can be written in reasonable time by an individual or small team.

And just one example (because it's one of my favourite topics where the C++ stdlib really failed):

std::string as a standard string type has so many (performance) problems that it is borderline useless. You can't simply use it anywhere without being very aware of the memory management implications (when and where does memory allocation and freeing happen - and std::string does its best to obscure such important details), and this isn't just an esoteric niche problem: https://groups.google.com/a/chromium.org/g/chromium-dev/c/EU...

Re:New C features in GCC 13 by nly on Friday May 05, 2023 @03:30AM (#35826318)
Classes, which have constructors and destructors, simplify code immensely..so do copy and move constructors and operators.