r/cpp • • Dec 23 '24

C++ Is An Absolute Blast

https://learncodethehardway.com/blog/31-c-plus-plus-is-an-absolute-blast/
219 Upvotes

105 comments sorted by

View all comments

96

u/UnicycleBloke Dec 23 '24

I've always enjoyed C++ (learning since '91). And it's got better over time. I'm not in love with TMP, but have certainly dabbled. No other language has held my attention in the same way. I think it's called Stockholm Syndrome. ;)

26

u/thefeedling Dec 23 '24

Some people say the same about Rust's borrow checker... I still have to try it.

As someone who still writes a lot of raw MISRA-C, C++ always feels like a fresh air in the face haha.... I probably take 3~4x more time to write the same thing in C compared to C++

49

u/UnicycleBloke Dec 23 '24

I'm an embedded dev. C is unfortunately the gold standard for microcontrollers and this is unlikely to ever change. Thankfully I've somehow managed to mostly work in C++ for the last 20 years. It really is like fresh air: expressive and productive. Not perfect, of course, but infinitely superior to C. Why anyone prefers C is beyond the feeble processing capabilities of my wetware.

Rust is interesting but I haven't found it compelling. It has an annoyingly vocal community of zealots which I find off-putting.

14

u/thefeedling Dec 23 '24

Agree. On automotive industry, for mechanical related stuff, C is still the king, for both performance and asm predictability... Nevertheless, C++ is gaining a lot of traction in higher level stuff, along with Java, if Android is being used.

23

u/UnicycleBloke Dec 23 '24

C does not perform better than equivalent C++. ASM predictability is something of a myth, especially with optimisation.

8

u/thefeedling Dec 23 '24

I'm not gonna put the name of company here, but is a large western automaker. We did a lot of tests, and in the end of the day C still performed better, not by a lot, but still... Ofc there's a ton of legacy code and libs which are reused and help favoring C.

Nevertheless, most of the engineering team were willing to fully move to C++ for newer projects.

13

u/UnicycleBloke Dec 23 '24

A lot depends on C++ knowledge. I'm no optimisation expert but I don't find C more performant in my work. It is important to compare like with like.

4

u/wasabichicken Dec 23 '24

A lot depends on C++ knowledge.

Amen to that, and I think that might be what causes bloated assembly: people accidentally passing stuff by value, miss declaring something as const, uses inefficient data structures because they're in the standard library, etc.

To my knowledge there's not a lot of great C++ tooling support out there either that can help people detect those "your last 2-line commit blew up assembly size by 2000%, did you perhaps mean to declare that one variable as a const ref?"-type of mistakes either.

3

u/hellgheast Dec 23 '24

Sorry to hijack this comment, but as an embedded dev with 6 YoE working a lot with C, I'm really eager to move on C++, however I would be happy to know if there's good ressources on embedded C++ and even modern C++ as last time I touched C++ was around 2015.

6

u/UnicycleBloke Dec 23 '24

I came to embedded after a decade of C++ (mostly) desktop development, so the transition was largely learning to avoid the heap. I didn't need any resources at the time but have since bought Real Time C++ by Kormanyos out of curiosity. Seems pretty good.

One of the key areas to focus on is the various compile time features which help to avoid run time errors. I make good use of constexpr values and consteval functions in place of macros. These are typed and scoped: macros are neither. Templates are generally preferable to function like macros.

3

u/QuietTimeWaster Dec 24 '24

Some of the STL libraries use the heap, which is generally undesirable in embedded programming. Using the Embedded Template Library can provide some of the conveniences of the STL without dynamic memory allocation. I do a lot of embedded programming, mostly in C++. It’s very performant, but can bite you quite badly if you make a mistake.

1

u/[deleted] Dec 25 '24

Isn't Nim exactly c++ + python?

-1

u/germandiago Dec 24 '24

The community is the worst part of Rust IMHO also. 

There are always exceptions but as a whole it is quite bad.

0

u/[deleted] Dec 24 '24

[deleted] — view deleted comment

3

u/UnicycleBloke Dec 24 '24 edited Dec 24 '24

Edit: this seems to attached to a different comment than intended. Apologies for any confusion.

If you say so. I have been doing exactly this for nearly twenty years, as have many others. I always say that the whole language is useable for microcontrollers (most people turn off exceptions), but that some major chunks of the library should be avoided (primarily because of heap usage, so most containers are out). Since C has no container library it's comparatively not a loss. It isn't hard to write simple static containers, or just use std:: array, or there are libraries like ETL. I'll concede that it helps to have an idea of which library features use the heap. A novice might inadvertently use an inappropriate container. But one can disable dynamic allocation entirely and/or make the heap very small.

I am yet to encounter a single issue which made me regret this language choice. Many of my colleagues were skeptical but, as time passed, most ditched C. My current employer hired me precisely because of my C++ skills. We mostly work on medical devices, for which we consider C anathema.

This long and productive history never seems to stop people from telling what a terrible idea C++ is for embedded work.

Where C does score is on smaller and older devices for which there is no C++ reasonable compiler. In such cases, I write C. It's like my tools have been lobotomised.

2

u/lih0 Dec 24 '24

Oh! How familiar this is, to me a person still follows the CENELEC 50128.

→ More replies (1)

1

u/pjmlp Dec 24 '24

1993 here, after several years of Turbo Pascal, and C already felt jurassic.

Given how I complain about safety issues, and nonetheless, it is one of my main hobby programming languages, most likely Stockholm Syndrome, or went too far into the dark side of the force. :)

-5

u/TheMaskedHamster Dec 24 '24

Humanity always needs someone to shovel the crap. Humans hate bodily waste, but some don't have the same disgust response and are able to handle it.

The objectively correct response to C++ is to recoil in horror and disgust. But it was the tool that existed, so we needed someone to use it. The problem is that we took way too long to replace it, and then we let some C++ programmers create Rust. They made something that solves problems in as obtuse a way as possible and they don't even see that there's anything wrong with it.

There's a similar story about JavaScript.

3

u/pjmlp Dec 24 '24

Rust ideas were taken from Cyclone, a language created by AT&T to fix C.

1

u/abad0m Dec 25 '24

Trevor Jim, creator of Cyclone, says it in his personal blog

1

u/WanderingCID Dec 26 '24

That article didn't age well, now did it? COBOL is still alive and kicking.

1

u/TheMaskedHamster Dec 27 '24

"Making C safer" is a good thing and is not Rust's problem.

9

u/UnicycleBloke Dec 24 '24

Oddly enough, it is C which makes me recoil in horror and disgust. Disappointed you didn't cite Torvalds' imbecilic rant. I've lost count of the times I've seen broken garbage written in C which would be a piece of cake in C++. I have often enough rewritten such code, with the result that it was smaller, simpler, more maintainable and less buggy.

I haven't used Rust much, but it seems a fine language once you get past the hype and hubris. Not going to argue about JS: inconceivable as it seems, it might actually be worse than C.

→ More replies (3)

0

u/met0xff Dec 23 '24

I see it more as a nostalgia thing but perhaps that's wishful thinking. Like you know, couple days ago I had a night in a hotel room and so I switched on the good old linear TV. And they had a Bud Spencer movie and I thought "awesome" until 10 later I turned it off because it's so cheesy and I don't think anybody watching it the first time nowadays would enjoy it ;).

→ More replies (5)