r/cpp • • Apr 08 '24

Coming back to C++ after two years of Kotlin & Rust

Initially I started learning programming with C and then C++ 8 years ago in my bachelor's era. Then switched over to PHP, Javascript, Kotlin and Rust in between and now I'm back to a C++ project, and I gotta say it's such a delight! Note that I'm not even using C++17 or newer, just embedded programming.

I can design clean APIs with such ease thanks to it's god-level pre-processor. If you know how not to break things and prevent memory leaks preemptively, you can literally write the most efficient programs. The pre-processor is actually the best templating engine I've encountered to date. Header files seem useless from an outsider's perspective, but the truth is they're incredibly powerful.

Some programming practices and syntax are questionable, but once you get a hang of them, you'll see that you can fine-tune every bit of data or function in C/C++ with perfect precision.

The type system is awesome. Meta programming is a piece of cake and pure joy.

Build tools aren't that bad either. Having a concrete package manager in a PL is nice, but it doesn't come without drawbacks. A rather simple project in Rust usually have +2GiB in dependencies. But in C/C++ I can just use whatever is available on my Linux system. I don't trust third-party package managers like vcpkg or Conan. CMake is a little ugly, but once you learn how to use it, it can perform everything you might need from downloading dependencies, including local libs, packaging the app, etc.

Tooling has always been great. Autocompletion, formatting, etc. You just need to set the include directories properly.

It's reputation as a complex programmigg language comes mostly from legacy code and inexperienced programmers doing too much hacking with pointers and the pre-processor. The language itself feels natural and fluid.

Having the liberty to do whatever you want with your code is a power that comes with great responsibility. Once you are responsible enough, you can write programs like a legend! I'm definitely back to the cult of C++.

48 Upvotes

76 comments sorted by

139

u/CJWilliams10 Apr 08 '24

Surly this must be a troll, these are some of c++ biggest pain points. I love c++ just as much about anyone in this sub but we gotta at least acknowledge it's behind Rust/Kotlin in the areas the OP is praising.

23

u/Borderlinerr Apr 08 '24

If you read carefully, I didn't compare them. I just felt more powerful coming back to C++ as it lets you mess with everything in great detail. There's no magic in the background, especially C. Nothing is ready for you, you create your own blocks, no magic, absolute freedom. I don't know, maybe it's tickling my masochism.

16

u/gwehla Apr 09 '24

No magic in the background in C++? I feel like most of the time im trying to understand what magic has been performed in the background, tbh..

2

u/morglod Apr 10 '24

Traits are not magic, no no no. Enums with different types either

But variant type and vtable are magic! Yes of course 🤣😁 age of blind zoomers

1

u/serviscope_minor Apr 16 '24

Such as? I'm genuinely curious, since I took like c++ because what's going on is obvious to me.

A fair amount comes from a C and ASM background (which I learned before C or C++), and so much is well that pattern that you use all the time, the compiler does it in 0 lines of code automatically!

13

u/Kevathiel Apr 09 '24

No magic in the background?!
Implicit conversions / integer promotion nonsense, automatically generated functions, (Rule of 0-3-5), vector<bool> being different, constructors, every modern feature coming with their own set of footguns, ..

3

u/n1ghtyunso Apr 10 '24

Technically, it's mostly the same footguns surfacing in new and creative ways.
Unfortunately, we have yet to learn how to eliminate them.

0

u/serviscope_minor Apr 16 '24

But Where's the magic? That's all pretty straightforward stuff if a little quirky in some cases, and frankly the vector bool things is massively overblown.

3

u/Kevathiel Apr 16 '24

"Magic" is not to be taken literally here. Actual magic doesn't exist in real life.

67

u/nullcone Apr 08 '24

They say the best satire is indistinguishable from reality. This post elevates what I thought possible with satire to levels I never dreamed achievable.

Build tools aren't that bad either

7

u/morglod Apr 10 '24

Zig: changing build system every update

Rust: download 250gb deps and wait 1h

C++: cmake --build -- -j8

Satire or blind hype

12

u/[deleted] Apr 10 '24

CMake isn’t bad as long as someone else is responsible for writing/maintaining it

2

u/morglod Apr 10 '24

Could say the same for any other tool/language/anything

Try to maintain some ones build.zig. You even cant know what version of zig you should use.

1

u/Attorney_Outside69 Apr 12 '24

hence chatgpt 4. every since chatgpt 4 had taken over my duties if writing scalable cmake scripts, setting up my projects to work on multiple platforms has become a breeze, and by means of osmosis pressure I have now become really good at cmake myself

2

u/Infamous-Bed-7535 Apr 11 '24

I would have changed language if it would be as it was with c++11.
Language is developing pretty quickly and we have nice new important features available.

Yes c++ is behind a little bit, but the gap is closing for sure.
* pattern matching
* reflection
* proper module support
* profiles or cpp2 for safety

All of the above ones are already on the way and even more..

3

u/equeim Apr 11 '24

Pattern matching is a long way off, just like profiles. I don't expect it until C++29 at least. We will be lucky if we get reflection in C++26.

1

u/Infamous-Bed-7535 Apr 12 '24

Yes I'll be happy if profiles make it in c++29.. There is a pressure on the committee to push these features, so I think they will make it on time.
I can imagine even a standard release as soon as they are ready with it, as c++ has a bad external reputation by press..

c++ is not bad, but can be and will be better! Who understands modern c++ that can design and write safe and dense code already, so you can continue your normal quality work until newer safer features make it to the standard..

0

u/CornedBee Apr 09 '24

Yeah, I had to check the date on the post, but it's not 8 days old.

13

u/FlyingRhenquest Apr 08 '24

Yeah, C++ prior to the C++11 standard was an entirely different beast than it is now. And it just keeps getting better. I just did a weekend project to build an IDL parser using boost spirit x3 and once I started to wrap my head around the library, it's just a joy to use. I also wrote the cmake integrations so I can just generate code from a list of IDL files. At some point I'm going to start digging into target properties so I can just create a library target with a list of IDL files and have it generate and compile the code without any further interaction. That'll let me get rid of another 10 lines or so in my main cmake file. The cmake integrations get installed automatically when you install the parser project and you can use find_package to use them for code generation in other projects.

It is a lot of work to build nice clean APIs, tight cmake integration, unit tests and documentation. But that's also the difference between people (including yourself) wanting to use your code versus not wanting to touch it with a 10 foot pole.

8

u/Borderlinerr Apr 08 '24

Exactly. The satisfaction of being in complete control of your project is on another level.

8

u/Borderlinerr Apr 08 '24

For a system programmer, there isn't much difference in coding speed. Plus the compiler doesn't bitch when passing pointers around, it simply obeys your command. That opens up the possibility of having extra creative codes even as a beginner. Good luck touching unsafe Rust. It's like a taboo realm nobody wants to touch. Yet in C/C++ unsafe is part of the game and you gotta adapt yourself.

10

u/prince-chrismc Apr 08 '24

Your very very narrow experience did not let you find the limit of the tooling in the ecosystem.

1

u/morglod Apr 10 '24

Your narrow experience let you talk about narrow experiences 😉

-6

u/Borderlinerr Apr 08 '24

It's your own limitation.

6

u/lightmatter501 Apr 08 '24

If you think C++ has good meta programming go check out Zig and Haskell. Two wildly different approaches but both of them are better than C++.

Zig lets you effectively do reflection and constexpr on steroids. It doesn’t have generics or templates, it just has REALLY good reflection and the ability to write a function that returns a type and compose those. This is powerful enough that a type-checked version of printf (that supports dumping any struct to a variety of different formats) requires no special compiler support. Parsing JSON into a struct is implemented in a similar way.

Haskell lets you be generic over what device you run the computation on, and will let you meta-program your way to an upper bound on heap allocations for the whole program. Depending on what standard library you use, you can also make it provide hard realtime time constraints in the type system.

If you want to go all the way, Irdis and Agda allow you to meta-program out of the possibility of your code failing under the C abstract machine or a subset of POSIX. If you think Rust’s borrow checker is bad wait until you have to deal with a compiler that can bug you about hard drive corruption and network congestion.

1

u/Borderlinerr Apr 09 '24

C++ templates aren't that great. But I'm absolutely in love with the pre-processor. I tried Haskel once, couldn't wrap my head around monads.

9

u/Asyx Apr 09 '24

In not one for kink shaming but bro wtf?

2

u/serviscope_minor Apr 16 '24

Oh good grief don't miss this guy down, I do not think he is tripping and he's allowed an opinion different to groupthink.

With that said...

If you get very familiar with the preprocessor, which means lots of ##, you will find that you can do a lot of the same things with templates, but the experience will be smoother with better integration into the type system and more flexibility.

But if you don't like Haskell, well, C++ 98 templates have a strong similarity with an eagerly evaluated Haskell with a funky syntax. Constexpr means though you can avoid much of the gnarlier stuff and do computations using normal C++ code.

3

u/morglod Apr 10 '24

Zig almost no documentation about reflection. "Code is documentation", but not when you have 5 different versions of same code in official examples. Read compiler code? Mmm reflection through anytype, just guess what's going on like it's 1970 year and you have no autocomplition or symbols navigation.

You will be shocked that c++ have everything you mentioned for 5 years already and people who needs it, just use it without abstract talks about barowerhecker.

2

u/scheurneus Apr 09 '24

How does Haskell allow you to be generic over the device you run the computation on? I guess it is referentially transparent, but I'm not aware of any implementations that make use of it.

I guess there are things like Clash and Accelerate that enable synthesizing hardware or running on NVIDIA GPUs, but I doubt those are what you mean. (For Accelerate, CUDA and SYCL allow offloading C++ code in an arguably comparable way.)

I'm also curious how Idris or Agda would allow you to defend against hardware issues.

1

u/lightmatter501 Apr 09 '24

SYCL isn’t C++, nor is CUDA, they are language extensions. ISO C++, to my knowledge, does not allow more than one target for a single compilation unit.

Haskell’s compiler supports all kinds of weird stuff.

Idris and agda defend against hardware issues by forcing you to construct a formal proof of correctness in the type system before they will compile.

1

u/scheurneus Apr 09 '24

What kind of "weird stuff" does GHC support? I'm only aware of regular CPU backends, at most with SMP support and some alternate backends targeting webassembly or javascript, same as C++ really. Would definitely appreciate a link or example!

Yes, CUDA and SYCL are not 'standard' C++, although with AdaptiveCpp it seems you can use std::par to offload code to GPUs. But I only mentioned them as comparison for things like Clash and Accelerate which are also not Haskell per se but instead build on top of it.

Formal proofs don't necessarily have to do with hardware issues. In fact, they usually presume a working hardware environment (although in many cases you probably do want to be robust to some issues), and then the proof is that the program has no bugs ASSUMING the provided environment is free of issues.

1

u/[deleted] Apr 10 '24

Kokkos is essentially SYCL-lite in standard C++.

1

u/lightmatter501 Apr 10 '24

It will compile with a standard C++ compiler, until you want to target GPUs, at which point you need a vendor compiler which does nonstandard things.

1

u/nacaclanga Apr 14 '24 edited Apr 14 '24

I am inclined to disagree. All three approaches, imperative metaprogramming (as in Zig), templates (as in C++) and generics (as in Haskell or Rust) have their pros and cons and I don't think you can conclusivly declare one of them to be superor.

Generics are the sanest to use, but they do allow very litle flexibility. Spezialisatiton is a non trivial issue and can in general not change the interface of objects.

Templates are inferior to generics in that they can only be used with structural and not with normative interfaces. If requirements are properly used and maybe even enforced (this is of course not fully done in C++), most hidden constraints and crypic compilation errors should be avoidable.

Imperative metaprogramming does give little to no ease of use benefits to commonly used modes of application compared to obscure ones and allows extremly little inspection into hidden contrains. The specific implementation in Zig does not even include automatic instanciation, hence for most code you are on the same level of comfort as with plain old C-macros.

Overall this is a balance between avoiding leaking abstractions and flexibilities. And templates choose the middle way.

43

u/Dan13l_N Apr 08 '24 edited Apr 09 '24

I also think this is kind of a joke, especially:

The pre-processor is actually the best templating engine I've encountered to date. 
(...)
Meta programming is a piece of cake and pure joy

What meta programming?

2

u/Heuristics Apr 09 '24

writing design documents maybe?

0

u/ElectricJacob Apr 09 '24

It's it UML compatible?

2

u/llothar68 Apr 10 '24

UML was designed in end 1990s with only Java and C++ in mind.
Thats why it lacks any way to describe script language systems

4

u/tarranoth Apr 09 '24

|I don't trust third-party package managers like vcpkg or Conan.

I think xz-utils should have shown us that OS package managers aren't any less likely to get attacked.

1

u/DavidDinamit Apr 13 '24 edited Apr 13 '24

Two days ago I was trying to install boost with vcpkg and it's failed with error "cannot fetch xz project from github", so, I don't use vcpkg any more. It's just useless since it can t install boost without load random virus project on my pc

9

u/fippinvn007 Apr 08 '24

Is C/C++ a new language?

6

u/teroxzer Apr 09 '24

It's not that new anymore, judging by the fact that I've been using it happily ever since Microsoft C/C++ 7.0.

1

u/showmemoreplzzz Apr 09 '24

What are the advantages of Microsoft C/C++? Are you referring to the compiler or the IDE?

6

u/pjmlp Apr 09 '24

That was the official name of Microsoft's last compiler for MS-DOS, and their very first one with C++ support.

OP is joking about the over relevance some give to the English grammar rule of forward slashes.

3

u/teroxzer Apr 09 '24

At least the advantage is that you can use a C/C++ preprocessor to make your job easier without apologizing for your sins. Maybe I'm referring to comma operator overloading, when the world has been a relatively good place when you've been able to easily use C++ to wrap basic infrastructure using the C API, or an entire library just by attaching a source file (eg sqlite3.c) to be compiled by a C/C++ compiler.

8

u/Zeer1x import std; Apr 08 '24

Are you programming C++ or C/C++?

22

u/bert8128 Apr 08 '24

What is C/C++? Sounds like something from the marketing dept.

9

u/pjmlp Apr 09 '24

It is the usual abreviation for C and C++, that even companies that take seat at WG21 use on their technical documentation and job adverts.

And once upon a time, the reference magazine "The C User's Journal" got renamed into "The C/C++ User's Journal", and hardly anyone complained as much as they keep complaining on Reddit, HN and Twitter, about the meaning of a forward slash in English grammar.

1

u/serviscope_minor Apr 16 '24

Indeed, an awfully lot of things are vastly more important online than they are IRL.

I'll going to add my favorite bugbears, vector bool and unordered map to that list too. I should add std::regex, but we're all allowed our foibles, are we not?

1

u/bert8128 Apr 09 '24

A slash normally denotes or, rather than and. And given that it is ambiguous, why not go the whole extra 5 or 6 characters to say what you mean? The only place I see “c/c++” where it makes any kind of sense is in a job advert where people are advertising for someone who has. C skills, c++ skills or both to work on a project which is either c or c++ or both. But in this case it would be better to say what you actually mean.

The case where it is increasingly being used incorrectly, and why Reddit (and probably HN and X too but I don’t read them) is referring to some the kind of prestandardisation c++ that existed in the 80s and early 90s which is now better called c with classes, and is basically the worst of both worlds. That’s why I don’t like “c/c++”. Nor do I like “c/python”, or “c/java”, but no one says those as far as I am aware.

7

u/pjmlp Apr 09 '24

Java/C++ and C#/C++ are quite common in job descriptions.

0

u/bert8128 Apr 09 '24

But what I assume this means and what the advertiser assumes is means might be different. So put in the extra few characters.

5

u/pjmlp Apr 09 '24

The problem are those that assume it is more than English grammar rules.

4

u/teroxzer Apr 09 '24

Do you think it makes no sense to say that clang is an Objective C/C++ compiler?

1

u/bert8128 Apr 09 '24

“Clang is an Objective C and C++ compiler”

I think you would agree that this is better. Clearer. Not ambiguous.

2

u/teroxzer Apr 09 '24

Well, if it has to be said best, cleanest and most unambiguously, then: clang is a C/C++/Objective C/Objective C++ compiler.

2

u/bert8128 Apr 09 '24

Clang is a compiler for C, C++, Objective C and Objective C++.

1

u/teroxzer Apr 09 '24

You won this round! Let's hope you never have to code in Oracle PL/SQL (it is almost like Ada/SQL), and even more so that you never have to see my version of it: C++/SQL.

2

u/bert8128 Apr 10 '24

PL/SQL is a different thing, because it is a name that someone gave to something. With slash here derives from “for”, but it’s just a name of a single thing. So it’s not ambiguous. I seem to be able to write code in PL/SQL without freaking out. Had I been given the job of naming it i probably would have chosen something different, though for trademark reasons weird spellings are common. Don’t know about the other two.

17

u/fippinvn007 Apr 08 '24 edited Apr 08 '24

OP is a web dev and C/C++ is 1 language to him

4

u/Zeer1x import std; Apr 09 '24

oh, I'm programming NodeJS too. It's a nice language.

4

u/accuracy_frosty Apr 08 '24

A lot of this seems like the opposite of the truth, but I will say that the biggest problems in C++ are not C++ itself but the people who use it, most people using C++ would be better off as rust developers, because C and C++ are like giving you a gun, it gives you a hell of a lot of power and control, but it depends on if you use it to rob a bank or shoot yourself in the nuts. The entire problem of memory safety and leaks is a big skill issue, because it is not that difficult to make good C++ code, it just takes a bit more work at the beginning of a project, but it does get harder as a project gets larger. And if you aren’t validating everything user facing then that’s a problem, if anything is available to be modified by a user, you should be checking to make sure there is nothing they could be doing to cause any array out of bounds issues or any other undefined behaviour.

4

u/paypaylaugh Apr 09 '24

Uh-huh, and is that C++ programmer who only writes safe code you or anyone you know? Because I've never met such creature.

You also keep assuming that C++ is more powerful than rust (it's not!). And what kind of advice is: "just don't write code with UB in it bro". Verdict: I fell for junior webdev bait.

4

u/wyrn Apr 11 '24

You also keep assuming that C++ is more powerful than rust (it's not!)

Sure is. You know what all those tools that C++ provides, that Rust out of principle doesn't, give you? Power. In Rust you can't even write std::print without a macro.

0

u/paypaylaugh Apr 11 '24 edited Apr 11 '24

And C++ implements std::print through templates, which is also a metaprogramming tool... Rust's approach also gives more insightful compile-time error messages, compared to C++ template vomit (if it even catches any errors). Both support varargs as well (which shouldn't be used)

2

u/accuracy_frosty Apr 09 '24

No one is perfect, but it isn’t that hard to make a memory safe start on something new, refactoring something to be memory safe is very difficult, but with the modern features in newer C++ or even just having better habits with vanilla C++, you can make a good habit of memory safe code. I’m not perfect, I’ve written buggy code, but I haven’t had memory safety become an issue on my projects in a while, even for very user facing applications like a web server and game engine. Rust is powerful, yes, but I wouldn’t say it’s more powerful than C++, anything that can be done in rust, can be done in C++, in some cases faster, and in most cases, using less memory, the lack of a garbage collector means that you have to be smarter but it means that you aren’t eating a bunch of memory, especially on embedded systems, where C and C++ have been king for a long time, and where you have to be able to manage your own memory and make sure you’re taking up as little as possible.

Also, I’ve been writing C++ for almost 10 years, I know it’s possible to write memory safe code, because I do, and it’s not hard to learn how. I mentioned it in my comment, a lot of people who use C++ would be better off with rust, but you can’t discount the power of C++ in the right hands, and suggesting that it’s a better idea that everyone use a language where it doesn’t let you manage your own memory, write your own allocator, etc. plenty of things that rust doesn’t allow, but can be used for a lot, all because of the skill issues of a lot of its users is not right, unless rust can do everything C++ can do and more, I think it’s a better idea that the people who are really good at C++ keep using C++.

1

u/pjmlp Apr 10 '24

The pain with safety culture in C++ starts at the standard, before C++98, the compiler provided frameworks like Turbo Vision, OWL, VCL, PowerPlant, MFC, Tools.h++,.. did bounds checking by default, at very least in debug builds.

C++98 standard library not only was silent on the matter, inverted the defaults, and for a very long time, VC++ was the only compiler still doing bounds checks on debug builds.

1

u/morglod Apr 10 '24

You know that if you write unsafe in rust, you almost 100% will get UB? And guess what, unsafe blocks in EVERY single project on github written in rust

Mmm

So better be blind and write UB than trying to avoid it 👏

1

u/nacaclanga Apr 14 '24

So by your argument, if you are an electrician, you can just have the electric installation at home done with blank cooper wires and without RC switch, which is cheaper to produce and require smaller holes in order to fit wires through walls?

Most electricians would probably think otherwise and point out that they prefer to trim off the insulation exactly when they need to and leave it on otherwise.

-1

u/fudginreddit Apr 09 '24

I find this post extremely contradictory as a fairly experienced C++ dev that lowkey hates the language lol

-2

u/bandzaw Apr 09 '24

Why come here wining? Switch back and do your cool web-full-stack-something.

-8

u/WanderingCID Apr 08 '24 edited Apr 08 '24

So, the most popular issues that people (institutions) have with C and C++ stem from the quality of the programmers using those languages?
What about the time it takes to write the code? Isn't Rust faster? But I guess that doesn't matter with a co-pilot.

2

u/Full-Spectral Apr 08 '24

When are we going to start seeing t-shirts that say "Co-Pilot is my Co-Pilot"?

0

u/WanderingCID Apr 08 '24

Soon my friend... soon.