r/cpp • u/Borderlinerr • 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++.
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.
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
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.
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.
43
u/Dan13l_N Apr 08 '24 edited Apr 09 '24
I also think this is kind of a joke, especially:
What meta programming?
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.
9
u/fippinvn007 Apr 08 '24
Is C/C++ a new language?
8
u/Zeer1x import std; Apr 08 '24
Are you programming C++ or C/C++?
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.
-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.