News for hackers. Stuff that matters.
Re:C2x: the next real revision of the C standard by Aardwolf on Monday November 12, 2018 @07:51AM (#18432288)
They should add something to run code when leaving a scope, no matter how you leave the scope (break, return, ...).

That would allow cleanup and other such actions without resorting to such hacks as: "goto cleanup" pattern, separate functions, fake for loop to break out of, ...

Re:C2x: the next real revision of the C standard by gkfasdfasdf on Monday November 12, 2018 @08:33AM (#18432470)
You can do this with GCC/CLang via the cleanup attribute: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attribute...

It would be nice to have this be a part of the standard.

Re:C2x: the next real revision of the C standard by jws on Monday November 12, 2018 @09:03AM (#18432622)
It's very useful. I use it for scoping the holding of locks. It replaces the idiom where you make a separate function to hold the 'when locked' code and pass all required state back and forth with nice clean inline blocks of code.
Re:C2x: the next real revision of the C standard by megous on Monday November 12, 2018 @08:26AM (#18432429)
You can do it with GNU C. It would be nice if it was standard.
Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @11:57AM (#18433771)
You can do it in Bjarne Stroustrup C, which is standard.

You can do it in the 20-year-old version of it, and draft versions many years before that.

Re:C2x: the next real revision of the C standard by sifoobar on Monday November 12, 2018 @02:29PM (#18434959)
C++ has about as much to do with C as Java with JavaScript these days though. It's just not an option in many cases where C is fine. I've gone back and forth the last 20 years or so, and have plenty of experience with both languages. Even the experts are beginning to loose faith, it's just getting too damn complex. I'll take variants as an example, the hoops people are willing to jump through to get something as basic as unions working properly is just insane. It looks more like religion than programming from here, Rust has the same cult like behaviors going on all over the place.

Edit: Ah, I see; you're more worried about them turning C into the next C++ :) I might even agree some days.

Re:C2x: the next real revision of the C standard by gpderetta on Monday November 12, 2018 @02:46PM (#18435097)
Please, C and C++ share a large subset, and, most importantly a memory model. Java and JS share, what, a couple of parentheses?
Re:C2x: the next real revision of the C standard by sifoobar on Monday November 12, 2018 @03:24PM (#18435404)
Sure, the interop is unmatched.

What they don't share is mindset; C is all about keeping it simple, I don't even know what C++ is about any more, but it sure isn't simple. Many fundamental tricks that are routinely pulled in C require jumping through plenty of hoops to keep a modern C++ compiler happy.

Speaking of interop, calling into C from other languages is very different from calling into C++. Yes, you can write C wrappers, but it's rarely done. And the further the language drifts into template lala land, the more tricky it gets.

Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @04:30PM (#18436042)
> Many fundamental tricks that are routinely pulled in C require jumping through plenty of hoops to keep a modern C++ compiler happy.

Not in my experience. Currently my man open source project consists of some 70,000 of C which compiles as C++. Maintaining C++ compatibility takes very little effort. I tend to switch to C++ before a release, to catch regressions. Often there is nothing. The most I spend is maybe five to ten minutes fixing some minor things. Some of those minor things have nothing to do with C versus C++ like signed/unsigned warnings.

Note that these signed/unsigned warnings from GNU C++ actually found a real problem: I had code which assumed WEOF was negative. (It was originally char based code that was switched to wchar_t; of course EOF constant in the narrow character world is negative.) The GNU C++ compiler also identified the situation in the same code that a variable that should have been wint_t was mistakenly int.

Re:C2x: the next real revision of the C standard by sifoobar on Monday November 12, 2018 @06:03PM (#18436808)
I'll repeat this here. Automatic void casts, flexible struct fields and calculating offsets to find the containing struct given a field are three things that I use all the time in C. Last time I checked, C++ wasn't very impressed by either one.
Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @10:31PM (#18438288)
The classic C90 version of the flexible array member (called the "struct hack") works fine in C++. So does the standard offsetof macro as well as containerof.

The classic struct hack looks like:

  struct header {
    size_t size;
    char data[1];  // not [0]
  };
the size of the structure to just before data is offsetof(struct header, data).

Automatic void casts are a misfeature in both languages; more so in C. In code that I control, I use a pointer to a character type as an "any memory" type, so that it requires a cast in both directions:

E.g.

  typedef unsigned char mem_t;
a custom allocator or allocator wrapper will return a pointer to mem_t.

I use macros for casts, which compile to the classic C cast in C, and the more restricted casts in C++:

  #ifdef __cplusplus
  #define strip_qual(TYPE, EXPR) (const_cast<TYPE>(EXPR))
  #define convert(TYPE, EXPR) (static_cast<TYPE>(EXPR))
  #define coerce(TYPE, EXPR) (reinterpret_cast<TYPE>(EXPR))
  #else
  #define strip_qual(TYPE, EXPR) ((TYPE) (EXPR))
  #define convert(TYPE, EXPR) ((TYPE) (EXPR))
  #define coerce(TYPE, EXPR) ((TYPE) (EXPR))
  #endif
The GNU C++ compiler has a cool feature: -Wold-style-cast. With this I can pinpoint uses of the casting notation, and then replace them with these macros.

The stupid void star thing and its conversion rules should never have been invented. C++'s treatment of it is more sane, at least, by working without a cast in only one direction.

What's really awful is official API's that use void star for opaque handles. The Kronos Group's OpenMax is one of these. You can mix up different kinds of handles for different objects and the calls will compile.

Re:C2x: the next real revision of the C standard by sifoobar on Tuesday November 13, 2018 @12:21AM (#18438793)
It's only a hack in C++ these days, from C90 it's standardized.

That's like your opinion, plenty of C programmers worth their salt would disagree with you.

Look, I'm not saying it's impossible to write code that compiles both as C and C++. The same is true of many other languages. The point is that you can't write C and expect it to be valid C++, which was the only thing I claimed.

Re:C2x: the next real revision of the C standard by monocasa on Monday November 12, 2018 @08:38PM (#18437684)
As someone who's been paid in the past to write shellcode, stuff like that paid my bills.
Re:C2x: the next real revision of the C standard by sifoobar on Tuesday November 13, 2018 @12:28AM (#18438822)
I'm pretty sure it was the same old regular buffer overflows as always. Which begs the question, what are you aiming for here?
Re:C2x: the next real revision of the C standard by monocasa on Tuesday November 13, 2018 @10:55AM (#18441880)
A lot of the time, simply overflowing a buffer like you would in the 90s doesn't cut it thanks to all of the mitigations built in these days. Finding code that completely forgets the shape of it's data really helps get the job done though.
Re:C2x: the next real revision of the C standard by ghettoimp on Monday November 12, 2018 @05:20PM (#18436497)
When I start doing anything in C, it is always refreshingly simple.

Then my project grows to the point where I need to do some simple string operations.

At which point I remember the wonderful simplicity of garbage collected, high level languages...

Re:C2x: the next real revision of the C standard by sifoobar on Monday November 12, 2018 @06:13PM (#18436871)
I use C directly when I have to, my preferred setup these days is embedding a more convenient scripting language in C.
Re:C2x: the next real revision of the C standard by stochastic_monk on Monday November 12, 2018 @03:12PM (#18435303)
Most C is inducted into C++ eventually. The point is that it would be nice to have something like RAII without subscribing to the full C++ perspective.
Re:C2x: the next real revision of the C standard by jcelerier on Monday November 12, 2018 @03:39PM (#18435537)
well, compile with a C++ compiler and just use RAII ?
Re:C2x: the next real revision of the C standard by sifoobar on Monday November 12, 2018 @03:43PM (#18435584)
It's not that easy.

A modern C++ compiler will have all sorts of issues with fundamental C idioms, and depending on C++ is a very different story from depending on C.

Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @04:24PM (#18435991)
C++ compilers do not have issues with fundamental C idioms.

I maintain a significant body of code which compiles as C or C++. The executable size and performance are about the same.

A C++ compiler will, of course, have "issues" with C99 and C11 features, that's for sure.

Re:C2x: the next real revision of the C standard by kevin_thibedeau on Monday November 12, 2018 @08:28PM (#18437626)
Running without exceptions is one C idiom. RAII, operator-new, and many other C++ features break down without them.
Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @10:37PM (#18438337)
Obviously, I do not use RAII, operator new or exceptions in a code base that compiles as C or C++.

Here let me note that I have not gone out of the way to disable anything in C++. I have not disabled EH in the compiler, or RTTI or anything. Yet, the executable size is close to the C one, and the performance is basically the same.

Re:C2x: the next real revision of the C standard by jcelerier on Tuesday November 13, 2018 @03:25AM (#18439513)
> Running without exceptions is one C idiom

well, did you know that for instance the windows C library was implemented in C++ ? and yet we don't see it throwing exceptions left and right.

Re:C2x: the next real revision of the C standard by danem on Monday November 12, 2018 @05:11PM (#18436428)
Which idioms are you referring to?
Re:C2x: the next real revision of the C standard by sifoobar on Monday November 12, 2018 @06:00PM (#18436789)
Automatic void casts, flexible struct fields and calculating offsets to find the containing struct given a field are three that I use all the time. Last time I checked, C++ wasn't very impressed by either one.
Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @10:39PM (#18438350)
Re:C2x: the next real revision of the C standard by yarrel on Monday November 12, 2018 @02:39PM (#18435035)
C, not C++.
Re:C2x: the next real revision of the C standard by v_lisivka on Monday November 12, 2018 @11:53AM (#18433736)

  /**
   * GCC and Clang extension to call cleanup function at exit
   * from the function with pointer to given function as argument.
   */
  #define defer(func) __attribute__((cleanup(func)))

  #ifdef _CRUST_TESTS
  
  // Sample destructor, to test defer
  static void _tst_charp_destroy(char * * value) {
    if(*value) {
      free(*value);
      *value = NULL;
    }
  }
  
  // Test case
  it(defer, "must deallocate object at the end of the function") {
    defer(_tst_charp_destroy) char * s = calloc(1, sizeof(char));
    (void)s;
  }
  
  #endif
Re:C2x: the next real revision of the C standard by MrRadar on Monday November 12, 2018 @12:34PM (#18434053)
They probably don't want to because then they'd have to specify how they would interact with the setjmp/longjmp functions (which are, in fact, specified as part of the ISO C standard, section 7.13 in C11).
Re:C2x: the next real revision of the C standard by int_19h on Monday November 12, 2018 @02:25PM (#18434935)
setjmp/longjmp already interacts poorly with other commonly used language features (e.g. values of non-volatile locals upon longjmp).
Re:C2x: the next real revision of the C standard by alxmdev on Monday November 12, 2018 @08:44PM (#18437716)
In case anyone is wondering, GCC and other compilers catch non-volatile locals clobbering when you compile with -Wextra.
Re:C2x: the next real revision of the C standard by CyberDildonics on Monday November 12, 2018 @11:45AM (#18433661)
I completely agree, I think this is something that C desperately needs.

D has scope(exit){...} which is similar to constructs like defer {...}

Many things can be done in some form with macros and the C11 generic keyword if they have to be, but defering statements to run on scope exit is still elusive as something that can be relied upon.

https://tour.dlang.org/tour/en/gems/scope-guards

Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @11:58AM (#18433772)
C desperately needs to be left alone.
Re:C2x: the next real revision of the C standard by dymk on Monday November 12, 2018 @12:07PM (#18433837)
You've made a few comments like this; care to explain your thoughts rather than just say a bunch of variations on "nooooo!"?
Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @01:27PM (#18434498)
The C language is finished and great as it is.

Many programmers like myself have no problem with it whatsoever.

Change in a key infrastructural component like this is only destabilizing.

The ISO C people should have the decency to disband and work on their pet language research projects in private.

Re:C2x: the next real revision of the C standard by dymk on Monday November 12, 2018 @02:40PM (#18435042)
The C language (and the C++ language, for that matter) basically _never_ make backwards incompatible changes. This is a first class priority for the standards committees.

Adding simple abstractions which don't hide complexity in the underlying assembly is a huge productivity and correctness boost. Not that it matters anyways; modern optimizers are always going to do things that absolutely bend your brain.

Wouldn't that be nice if you could use a `scope(exit) {}` instead of accidentally missing two or three `goto cleanup_7;` which result in an RCE?

I would absolutely love a standards-compliant way to scope-guard sections of code. It'd make huge swaths of C code simpler; it's even in enough demand that Clang and GCC implement as their own extensions.

Re:C2x: the next real revision of the C standard by pjmlp on Monday November 12, 2018 @04:24PM (#18435989)
Better go through the standards then, both of them have introduced breaking changes in their revisions.

I can gladly provide a couple of examples.

Re:C2x: the next real revision of the C standard by dymk on Monday November 12, 2018 @10:39PM (#18438354)
Please do provide examples. Revisions to the C and C++ have almost never broken meaningful backwards compatibility, at least at the language semantics level.
Re:C2x: the next real revision of the C standard by pjmlp on Tuesday November 13, 2018 @02:21AM (#18439263)
Besides what nrclark has written on his/her comment, C11 dropped gets() and the introduction of the new memory model in C11 might break code that was relying on CPU specific semantics not in synch with it.

Regarding C++:

- auto changed its meaning on C++11

- export templates were removed in C++11

- exception specifications were deprecated in C++11, removed in C++17 and might do a come back in C++23 with value based exceptions

- gets() got removed in C++11

- declspec and auto had a small semantic difference, settled in C++17

- initializer lists introduced in C++11 changed their auto deduction type in C++17

- the required implementation semantics for std::string in C++11 broke COW in compilers like GCC.

Just a small list, there are a couple more.

Re:C2x: the next real revision of the C standard by nrclark on Monday November 12, 2018 @11:21PM (#18438567)
C's variable-length arrays are one example. They were introduced in C99, and downgraded to 'optional' in C11. So that's one place where a newer C compiler could refuse old C code and still be language compliant.

Also the draft C2x standard does away with K&R declarations, which will be another compatibility break.

Re:C2x: the next real revision of the C standard by aseipp on Monday November 12, 2018 @03:44PM (#18435594)
Here is a counterpoint: The C language is not finished, and many programmers like myself would like to see the language continue to evolve and grow in reasonable ways, especially given the fact we'll keep on writing it for a while to come. The ISO C people should be commended for doing their hard work.

Alright. Glad we cleared this whole thing up.

Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @04:18PM (#18435934)
> we'll keep on writing it for a while to come

Nope; I will be writing in "it". What you obviously want is to be writing in something other than "it". You basically want to be able to write Rust, Go or D into a file that has a .c suffix and is passed through a preprocessor.

Re:C2x: the next real revision of the C standard by ArchTypical on Monday November 12, 2018 @03:22PM (#18435385)
> The C language is finished

There's no evidence of this. There's just existing code. Ok. There's existing code in lots of other languages with different syntax (large and small differences). So what?

The latest video about Oden was a fantastic primer on QoL changes that should be standard, but there are people who always think what they learned is optimal. These are the same people who trivialize evidence to the contrary, in defense of their particular viewpoint.

Re:C2x: the next real revision of the C standard by jononor on Monday November 12, 2018 @02:17PM (#18434858)
You are free to stay on C89 or C99. They are not going away anytime soon, too much code relies on it.
Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @04:55PM (#18436271)
If you think if standard-driven changes to the compiler will never break your C89 code, because you're using the -std=c89 switch or whatever, you are pretty naive.

The only guaranteed way your compiler won't break your code is if it's left alone.

Re:C2x: the next real revision of the C standard by jononor on Monday November 12, 2018 @06:03PM (#18436812)
That can happen due to changes unrelated to new standards too. So if you are worried about compiler changes, then definitely freeze the compiler version you use. Alternatively invest in in quality assurance to verify that your code still works with a new compiler version.
Re:C2x: the next real revision of the C standard by scarejunba on Monday November 12, 2018 @02:20PM (#18434888)
The standard is versioned. You can always use a modern compiler targeting an older standard. You don't have to use C11. Or C21.
Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @04:07PM (#18435830)
And then there is this activity called "working with other people".

Also, gratuitous changes to the compiler can break things that affect C90 operation. There is a risk.

Re:C2x: the next real revision of the C standard by rbanffy on Monday November 12, 2018 @04:10PM (#18435862)
Or go back to K&R, for that matter.
Re:C2x: the next real revision of the C standard by isaachier on Monday November 12, 2018 @09:50PM (#18438065)
Once you do that, you more or less recreate C++. IMO C vs. C++ is fundamentally an issue of RAII and arguably templates. This proposal would add RAII to C.
Re:C2x: the next real revision of the C standard by anticensor on Tuesday November 13, 2018 @07:24AM (#18440417)
You have seem to have left C++ in 1999. C++ has expanded so much that someone can create its own operating system just by adding drivers around its standard library.
Re:C2x: the next real revision of the C standard by plop_ on Monday November 12, 2018 @09:33AM (#18432784)
Someone should submit a proposal.
Re:C2x: the next real revision of the C standard by grive on Monday November 12, 2018 @05:24PM (#18436528)
The hacks you have listed have the advantage to be visible. Reading the code, you know whether they are called or not.

The issue with cleanup on scope exit is that this introduces ambiguous code-flows. You would need to demonstrate a strong advantage, beyond making life easier for the writer of the code.

Re:C2x: the next real revision of the C standard by majewsky on Monday November 12, 2018 @07:52AM (#18432292)
The idiomatic way to do that is with destructors.

EDIT: Sorry for the confusion, I misread the article and thought this was about C++2x.

Re:C2x: the next real revision of the C standard by Aardwolf on Monday November 12, 2018 @08:11AM (#18432372)
Yep, talking about C here, not C++.

If they would add that feature, you could use macros to get constructor and destructor like behavior (as long as it's within the same scope, you could write a macro that constructs your object, and in addition contains something like "at scope exit: run this function"), which would be super useful and as far as I'm aware not very complex for the compiler (as all they have to do is run some code whenever exiting scope) nor very far outside of the spirit of C (it's still very low level, just a small convenience feature with interesting implications, especially when used with macros).

Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @12:04PM (#18433818)
C++ is a dialect of C.

It is the result of starting with C, and then accommodating a massive, sustained sequence of feature requests, exactly like ones we are seeing in this thread.

Everyone thinks that if just their pet feature request is added to C and everyone else's ideas are rejected, it will still be C.

Re:C2x: the next real revision of the C standard by bunderbunder on Monday November 12, 2018 @12:50PM (#18434182)
C++ is more than that; it's a set of additional features that create something of a C-flavored multiparadigm language. Which inevitably does turn it into a kitchen sink language, since it's trying to be all things to all people.

This idea doesn't seem to be in that spirit to me. It's not trying to do something radical like, say, adding OOP semantics to C. It's suggesting that C, a structured programming language, get a feature designed to improve the ergonomics for people doing structured programming in C.

Re:C2x: the next real revision of the C standard by mouldysammich on Monday November 12, 2018 @07:54AM (#18432299)
C doesn't have destructors, does it?
Re:C2x: the next real revision of the C standard by stinos on Monday November 12, 2018 @08:04AM (#18432334)
You can use C++ as some kind of C with destructors though :P Scoped cleanup is really a nice thing..

But then you also want templates of course to get rid of your void*. And oh, wait, what's that? A cross-platform thread implementation (well, sort of, depends on platform)? A string thing which I can search without strstr? Anonymous functions which capture? Auto? Gimme gimme :]

Re:C2x: the next real revision of the C standard by beefhash on Monday November 12, 2018 @08:32AM (#18432460)
> You can use C++ as some kind of C with destructors though :P Scoped cleanup is really a nice thing..

C++ is not a strict superset of C. You actually have to cast void pointers, for example.

> But then you also want templates of course to get rid of your void*.

No, actually, I think I can live without that, but thank you.

> And oh, wait, what's that? A cross-platform thread implementation (well, sort of, depends on platform)?

C11, threads.h. Been there, got that.

> A string thing which I can search without strstr?

Okay, I'll give you that, string handling is hell.

> Anonymous functions which capture?

Have their uses, but I'm not sure if C is really the right place for it.

> Auto?

auto (as used in C++11 and later) is a solution in need of a problem in C. When you don't have piles of templates and iterators, you also tend not to need to automatically derive types. In a world where signed/unsigned integer comparison can have fatal consequences, you probably do want to be very sure about your types.

Re:C2x: the next real revision of the C standard by stinos on Monday November 12, 2018 @11:50AM (#18433697)
> C++ is not a strict superset of C.

I know that. I use both C and C++ practically daily. The post was partly joking. But only partly (for example of course I could live without templates if really needed but seriously they make some jobs a lot better and easier). Hence I aptly phrased it some kind of C hoping (idly it seems) no one would trip over it because it's obviously not C.

Re:C2x: the next real revision of the C standard by rubber_duck on Monday November 12, 2018 @08:56AM (#18432587)
>C++ is not a strict subset of C.

I think you flipped this :) ?

Re:C2x: the next real revision of the C standard by saagarjha on Monday November 12, 2018 @09:02AM (#18432616)
I mean, it’s not wrong…
Re:C2x: the next real revision of the C standard by beefhash on Monday November 12, 2018 @09:59AM (#18432950)
Oops. Edited a fix in.
Re:C2x: the next real revision of the C standard by maxxxxx on Monday November 12, 2018 @11:17AM (#18433459)
Sometimes I wonder if a C++ without inheritance would be the right thing. All the crazy design abuses I have seen were around inheritance. Other things like auto and anonymous functions are just super useful and I wouldn't want to miss them.
Re:C2x: the next real revision of the C standard by Dylan16807 on Monday November 12, 2018 @09:25AM (#18432741)
I don't think those other features are half as compelling as you think they are.
Re:C2x: the next real revision of the C standard by stinos on Monday November 12, 2018 @11:41AM (#18433623)
Using both C and C++, each in their place, I actually do think they are.
Re:C2x: the next real revision of the C standard by Dylan16807 on Monday November 12, 2018 @06:07PM (#18436842)
Then why ever use C if they're that compelling? I'm not really understanding how wanting one little thing from C++ implies diving all the way in.
Re:C2x: the next real revision of the C standard by stinos on Tuesday November 13, 2018 @03:53AM (#18439624)
Then why ever use C

Because some platforms I've used it on did't have proper C++ support. Or because a project I contribute to is already written in C. Or because of limited resources. Etc.

I'm not really understanding how wanting one little thing from C++ implies diving all the way in.

It's just an example of a possible train of thought. And I wouldn't call something like templates 'one little thing'. So suppose there's a choice between both, and no limit on resources etc, and I could go for everything C but templates could make everything easier then just going for C++ instead is an option. Even when not going 'all in' and writing what looks like standard C + template support.

Re:C2x: the next real revision of the C standard by Dylan16807 on Tuesday November 13, 2018 @04:49AM (#18439832)
> And I wouldn't call something like templates 'one little thing'.

Agreed. But templates aren't the one little thing, they're something you said would be 'next'. The 'one little thing' is a way to replace the GOTOs used for releasing resources. I think it's an easy argument that when people are repeatedly building a pattern out of GOTO, there's a gap in the control flow tools that a language gives you.

Re:C2x: the next real revision of the C standard by jfoutz on Monday November 12, 2018 @12:04PM (#18433822)
Auto is already taken in C. It’s the default, rather than register or static, so you hardly ever see it
Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @01:49PM (#18434657)
auto was taken in exactly the same way in C++ until fairly recently. They re-purposed it because it was obsolescent.

auto was already in vanishing disuse in C at the time ANSI C came out in 1989.

The C++ use of auto resurrects an aspect of the way auto was used in the C predecessor languages B and NB.

In these pre-C languages, everything was an integer cell. A declaration of local variables looked like this:

   auto x, y;
the auto meant automatic storage (stack), but the real purpose here is syntactic: the auto is a declaration specifier, which makes the above construct a declaration. There is no indication of type, because it is implicit. Without the auto it would look like a comma expression with no effect.

This implicit typing survived into C for "implicit int" return values, like "main () {...}". But type specifiers were introduced and became mandatory in declarations, and so auto became unnecessary verbiage since all it did now was specify a storage class that was already the default one.

C++ resurrect the syntactic use of auto in a way: once again, it provides declaration syntax that allows the type specifier to be omitted. This time, of course, that type is inferred rather than assumed integer.

Re:C2x: the next real revision of the C standard by jfoutz on Monday November 12, 2018 @02:20PM (#18434883)
This is a great thoughtful reply to my terse, shallow comment. Thank you. It’s insightful
Re:C2x: the next real revision of the C standard by shaklee3 on Tuesday November 13, 2018 @12:46AM (#18438906)
Not the language, but some compilers have something similar:

https://stackoverflow.com/questions/2053029/how-exactly-does...

Re:C2x: the next real revision of the C standard by vkazanov on Monday November 12, 2018 @07:55AM (#18432304)
Yeah... Actually adding something like defer {...clean up code...} would be really nice.

But who are we to ask..? Just programmers, i.e. mere mortals nobody really cares about. :-)

Re:C2x: the next real revision of the C standard by MaxBarraclough on Monday November 12, 2018 @08:58AM (#18432604)
Brief aside: Boost has a facility for doing this in C++ without declaring a new class. (It boils down to a destructor, but it's more compact.)

https://www.boost.org/doc/libs/release/libs/scope_exit/doc/h...

Re:C2x: the next real revision of the C standard by nwallin on Monday November 12, 2018 @08:17PM (#18437571)
Do they have a new version based on lambdas? The syntax of that is painful. I splatter my code with this:

  // WTFPL
  class Finally {
    std::function<void()> f;
  public:
    Finally(std::function<void()> f) : f{f} {}
    ~Finally() {f();}
    void disable() { f = [](){}; }
  };
Which looks much prettier:

  socket* s = open_socket(); // or whatever
  Finally cleanup_socket{[](){ close_socket(s);}};

  // optionally, to keep it alive:
  cleanup_socket.disable();
Am typing on mobile. No guarantees about syntax errors. I still need to look into where the right places to put r value references and/or std::move are, but for the most part, I never put anything other than a pointer in the closure so I'm not worried about copy constructor cost.
Re:C2x: the next real revision of the C standard by Matthias247 on Monday November 12, 2018 @10:05PM (#18438144)
However your implementation based on std::function might allocate, which is then non-zero cost compared to normal exit statements. Better directly store the lambda inside the class, which requires that to be generic (Finally<LambdaType>). That can be hidden by using the class via a templated function which uses types interference:

    auto cleanup_socket = make_scope_guard([&]{ close_socket(s); });
Searching for scope_guard yields lots of alternative implementations.
Re:C2x: the next real revision of the C standard by frankzinger on Tuesday November 13, 2018 @12:40AM (#18438865)
make_* functions are no longer necessary in C++17 which adds "Class template argument deduction" [1]. You can now do

  std::pair p{"aaa", 123};
and

  Finally guard{[]{cleanup();}};
without specifying template arguments.

[1] https://en.cppreference.com/w/cpp/language/class_template_ar...

Re:C2x: the next real revision of the C standard by Matthias247 on Wednesday November 14, 2018 @02:45AM (#18448254)
Thx, I wasn't aware about this feature yet!
Re:C2x: the next real revision of the C standard by aaaaaaaaaab on Monday November 12, 2018 @03:05PM (#18435237)
Boost.ScopeExit does declare a new class via macros.
Re:C2x: the next real revision of the C standard by MaxBarraclough on Monday November 12, 2018 @05:24PM (#18436524)
Yes, and I hinted at this, but it's more compact.
Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @11:57AM (#18433763)
> They should add something

I disagree.

> to run code when leaving a scope, no matter how you leave

This, and various other features, were already added to C long ago. The resulting dialect was called C++.

Re:C2x: the next real revision of the C standard by bunderbunder on Monday November 12, 2018 @12:16PM (#18433902)
I'm not sure I understand what you're getting at here.

Are you implying that people who want new features in C should just migrate to C++ because it has even more features? Or that C is perfect as-is and anyone who wants to add new features to it should bug off to another language? Or that this particular feature is somehow equivalent to (or a slippery slope toward) adding a whole set of OOP features to C, so we might as well take that to its logical conclusion?

Re:C2x: the next real revision of the C standard by kazinator on Monday November 12, 2018 @01:22PM (#18434460)
Pretty much all of the above.
Re:C2x: the next real revision of the C standard by bunderbunder on Monday November 12, 2018 @02:09PM (#18434803)
I'd like to hear you elaborate. You seem to be very invested in your concerns, but it's hard to understand exactly what they are.

A single, clearly articulated response would do a lot more than a fusillade of terse barbs to help others understand why you think this is a bad idea.