News for hackers. Stuff that matters.
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)