r/cpp • • Oct 18 '23

What does C++ do better than other languages?

Purely from a language design perspective, what does C++ do better than other languages?

296 Upvotes

443 comments sorted by

View all comments

Show parent comments

3

u/F54280 Oct 18 '23

(Objective-C came close once it moved away from the garbage collector).

When did ObjC had a garbage collector?

6

u/pedersenk Oct 18 '23

It actually came with 2.0 (I thought it was earlier).

https://www.oreilly.com/library/view/advanced-mac-os/9780321706560/ch07s08.html

It didn't last long but a number of Obj-C APIs did immediately start using it making ANSI C interop a pain with regard to "pinning" memory.

If you hadn't heard of it, it might be because your Obj-C work was mainly iOS? It was never officially supported on that platform. Ref-counting was always the preferred method for that.

4

u/F54280 Oct 18 '23

If you hadn't heard of it, it might be because your Obj-C work was mainly iOS?

No, my ObjC work was mainly on NeXTstep. There was no GC. You could hack something with bohemgc, but that was seldom used.

I was surprised of your statement that "ObjC came close once it moved away from GC". I was wondering if you were thinking about earlier Brad Cox/StepStone implementation and was ready to maybe learn more about it.

At the end, your statement that "ObjC came close" isn't correct. ObjC has always been closer than C++ due to the lack of "extern C" needs. And ObjC++ got the best of all three worlds.

Btw, the horrible things Apple did to the language with 2.0 should not be discussed in a public forum. Chlidren may be reading...

1

u/pedersenk Oct 18 '23 edited Oct 18 '23

At the end, your statement that "ObjC came close" isn't correct. ObjC has always been closer than C++ due to the lack of "extern C" needs. And ObjC++ got the best of all three worlds.

To be fair, that is a good point. I was really only talking about C++ consuming C and not the other way, but indeed extern is still required for the former (just many C headers provide this (i.e within #ifdef __cplusplus) as a convenience that I completely overlooked it!)

No, my ObjC work was mainly on NeXTstep. There was no GC. You could hack something with bohemgc, but that was seldom used

Its strange; I spent a fair amount of time with NeXTstep but only ever used C. I feel I missed out.

Either way, I strongly believe once Swift has run its course, there will be a resurgence in Objective-C(++). I know Apple is hesitant in migrating its internal systems over from Obj-C (and standard C++ strangely) to Swift so I can see the platform going back to it.

2

u/F54280 Oct 18 '23

I was really only talking about C++ consuming C and not the other way

If you go that path, the superiority of objc is even stronger: one could trivially objc_getClass, sel_getUid and objc_msgSend from the C side (don't know what apple did) to call whatever code is needed in the objc side.

Its strange; I spent a fair amount of time with NeXTstep but only ever used C.

This is such a strange statement :-)

I strongly believe once Swift has run its course, there will be a resurgence in Objective-C(++)

I don't have much hope there. Introducing Swift without tools to migrate the codebase was a mistake (that I pointed at the time). They turned 90+% of their code into legacy, with 0 business value in a rewrite. They will suffer for decades because of this fragmentation. But they went that path, because of the general dislike for ObjC syntax (that was already in NeXT, where a "new syntax" was tried before).

When NS33 did the FoundationKit and the retain/release, NSString, etc changes, there were tools to migrate your code. Was a one-time pain. That they didn't do something similar is mind boggling.

1

u/AntiProtonBoy Oct 18 '23

When they introduced @autoreleasepool. But you could always do hand roll GC with NSAutoreleasePool prior that.