Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

IMHO the one great feature of Objective-C (compared to C++) is that it doesn't interfere with any C language features. In C++ the C 'subset' is stuck in the mid-1990s, while Objective-C "just works" with any recent C standard.
 help



The one really funny feature of Objective-C++ is that it lets you write C++ using modern C features that haven't been pulled into C++, and you don't have to actually use the Objective part. Designated initializers before C++ got them were the main actually useful application of this.

Interestingly, I recently auto-translated wget from C to a memory-safe subset of C++ [1], which involves the intermediate step of auto-converting from C to the subset of C that will also compile under clang++. You end up with a bunch of clang++ warnings about various things being C11 extensions and not ISO C++ compliant, but it does compile.

[1] https://duneroadrunner.github.io/scpp_articles/PoC_autotrans...


I think C++ have caught up with C99 already. So it's late 90s, not mid-90s :)

No not really, for instance the designated init feature in C++20 is a distinctive C++ feature which is not compatible with C99's designated init. AFAIK the C subset hasn't changed since C++98.

What C features can you not realistically use from C++?

C11 atomics, C11 threads, variable length arrays, safely reading from an inactive union member, designated array initializers, compound struct literals, implicitly converting a void pointer to a typed one, and the list goes on.

C++ has its own atomics, threads etc. And variable length array's work just fine in C++ in both clang and GCC.

C++ also has it's own designated initializer like syntax.


>C++ has its own atomics, threads etc.

It does indeed, but they are not compatible and at least the C atomics cannot be used from C++.

>C++ also has it's own designated initializer like syntax.

One that is far more limited. This is valid C, but not C++.

  struct bar {
      int a;
      int b;
  };
  
  struct bar tmp = {
      .b = 0,
      .a = 0,
  };

Indeed, and some of those, thankfully as C++ is TypeScript for C.

Any language feature added in C99 and later.

In GCC and Clang it's quite relaxed since their C++ frontends pull in a couple of modern C features as non-standard C++ extensions, but MSVC has a strict separation between the C++ and C frontend and doesn't allow this sort of 'cross-pollution'.

As a quick example, this C99 code block has at least 4 features that don't work in C++:

https://github.com/floooh/sokol-samples/blob/91bd8c6f9f52149...

- taking the address of an adhoc-created struct in a function call

- random array access in struct initialization

- designator chaining in struct initialization

- designators can be out-of-order in C but not C++

...also using unions as different views on the same memory is actually useful sometimes.


Nothing of value was lost

People shoudld realize that since long ago C and C++ are not sub/supersets, but interesecting sets.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: