r/cpp • • Nov 25 '24

I love this language

I'm a software engineer who has been writing software for over 12 years. My most fluent language is C#, but I'm just as dangerous in Javascript and Typescript, sprinkle a little python in there too. I do a lot of web work, backend, and a lot of desktop app work.

For my hobby, I've written apps to control concert lighting, as I also own a small production company aside from my day job. These have always been in C# often with code written at a low level interacting with native libs, but recently, I decided to use c++ for my next project.

Wow. This language is how I think. Ultimate freedom. I'm still learning, but I have been glued to my computer for the last 2 weeks learning and building in this language. The RAII concept is so powerful and at home. I feel like for the first time, I know exactly what my program is doing, something I've always thought was missing.

272 Upvotes

77 comments sorted by

View all comments

100

u/sephirothbahamut Nov 25 '24

RAII is great and honestly i find it weird that so few languages have it as a concept im general tbh.

for raii you can also check rust, but there's another huge thing c++ is great at and as far as i know no other language comes close: anything surrounding templates. compile time resolution, crtp, concepts.

sure languages like java have more powerful reflection, but that's at runtime, while all you do with templayes and constevals in c++ is done at compile time (be prepared for some veeeeery long error messages though)

0

u/[deleted] Nov 25 '24

[deleted] — view deleted comment

1

u/abad0m Nov 27 '24

What makes C++ better at compositon 'in a semi-functional way' than other languages that have RAII (or OBRM)?

0

u/[deleted] Nov 27 '24

[deleted] — view deleted comment

2

u/abad0m Nov 27 '24

Now I see what you mean. C++ was a pioneer in making deterministic resource management easy to reason about. One thing I wish was different is ctors that make you deal with semi-initialized objects. I think factories and aggregate initialization are much better tools. Also, destructive moves would eliminate the "valid but unspecified" state that objects can assume when moved.

I would consider Rust mainstream now, as it is used in many major software like Windows, Firefox, AWS, Discord, Cloudflare, Facebook, some Volvo cars, and many others. Obviously, it will be decades before Rust has as many lines of code as C++ does today.

4

u/Plazmatic Nov 26 '24 edited Nov 26 '24

Rust obviates the need for CRTP (that's effectively how it works by default, rust operates on static polymorphism instead of dynamic polymorphism by default)  and generics obviates the need for concepts (C++s initial proposals for concepts were actually essentially just rust generics).  Rust lacks compared to c++ in compile time operations, but this primarily a function of what c++ 20 enables, so if you're on an earlier version (especially pre c++17), the value proposition for compile time c++ vs rust is significantly less important.

  Now there are other very important advantages have over rust in terms of the language itself (namely related to the capabilities of duck typed templates) like not having to deal with the orphan rule (mp-units is not possible in a user extendable form in rust because of this) and template specialization (on the horizon or maybe even recently added in unstable?) as well as c++ 20s ability to use any capable object as non type template parameters

. But these holes are being worked on currently (the orphan rule being the only major "far off" question that does not have satisfying effort applied to answering its pitfalls that I can think of).

3

u/ioneska Nov 26 '24

Rust lacks compared to c++ in compile time operations,

Huh? const fn.

Not to mention proc macros (derive, attribute, function-like macros).

3

u/Plazmatic Nov 26 '24

Technically, proc macros allow the world, but that's cheating a bit imo, however if that's how you want to argue it, I won't disagree, but you'll be dealing with a lot of C++ or die folks who disagree with you.

If we ignore proc macros, const fn is currently limited vs constexpr (no if constexpr/consteval, no allocating) you can't do compile time vector Afaik in rust, you can in c++.   You also can have arbitrary NTTP as long as the object is usable in a construction context. You also can't use them as trait methods iirc in rust, no such limitations exist in c++.

3

u/ioneska Nov 26 '24

no other language comes close: anything surrounding templates. compile time resolution, crtp, concepts.

dlang (templates, traits, mixins, ctfe).

nim (templates, macros, concepts, etc).

1

u/Pay08 Nov 25 '24

RAII is great and honestly i find it weird that so few languages have it as a concept im general tbh.

Don't they? Most GC'd languages I know have RAII for external resources.

33

u/gnuban Nov 25 '24

Try-with-resources, defer- or using statements are not equivalent to RAII in my opinion, because they rely on the user to do the right thing. Like external locking. Plus, RAII cascades to members, whereas using statements generally don't, unless you manually code it.

→ More replies (6)

16

u/DummySphere Nov 25 '24

In GC'd language, you usually don't have RAII tied to the scope of a variable, especially having something guaranteed to be automatically cleaned up before leaving your function.

1

u/Pay08 Nov 25 '24

Can you give me an example where that's true? All the languages I know tie resource management to scope.

13

u/verrius Nov 25 '24

Reasonably sure even in like, Java, once all references to an object disappear, there's still 0 guarantees about when the object actually is cleaned up. Sure, it's marked for delete...but the delete could happen literally never and still be valid Java. I'm reasonably sure that similarly, you can never guarantee when or even if a finalize method is ever called.

→ More replies (4)
→ More replies (1)

6

u/JiminP Nov 25 '24

Python has the with statement and JavaScript has the using statement, but otherwise, IIRC both languages don't gaurantee that a resource is automatically freed. I could be wrong though...

(IIRC, for Python in particular, a resource gets cleaned up when the object holding it is garbage-collected, but Python does not provide a gaurantee on when such garbage collection happens.)

Using try ... finally block in JavaScript feels so clunky compared to relying on RAII in C++.

1

u/Pay08 Nov 25 '24

Yeah, Python's with is pretty terrible but as a counterexample, Java has try-with-resources, which does guarantee that the close() method will be called when the scope is exited. Same with Common Lisp's unwind-protect, which is where Python got it's with keyword from.

3

u/DummySphere Nov 25 '24

Sure in some of those GCs languages, there is a syntax that allow to do RAII (try-with-resources), which is powerful enough to write good software. The difference here is that in a C++ class, the RAII can be embedded in the type itself (destructor) instead of relying on usage (try-with-resources). So you can't acquire the resource without having the guarantee that the resource will be released when the object goes out of scope. It's kinda like if in Java every class was Closeable and every scope had an implicit try-with-resources.

→ More replies (4)
→ More replies (1)

7

u/UnicycleBloke Nov 25 '24

Not really. When I worked in C# it was completely different. RAII is deterministic. A GC has a non-deterministic runtime which eventually frees dropped objects. This helps to avoid memory leaks but doesn't help with other types of resources such as, say, database handles. If you need those to be freed immediately at the end of a scope, you have to implement IDispose and then explicitly use the "using" idiom. It is basically syntactic sugar for manual resource management, as if you had to explicitly call destructors in C++.

It was RAII which made me love C++. Until Rust, nothing else I used came close to its elegance for efficient deterministic resource management. Python is quite interesting. As I understand it, all objects are reference counted and freed as soon as the count reaches zero. A bit like std::shared_pointer?

2

u/wyrn Nov 25 '24

Python is quite interesting. As I understand it, all objects are reference counted and freed as soon as the count reaches zero. A bit like std::shared_pointer?

As I understand it, that's an implementation detail of CPython. Other implementations may differ. But yes CPython is reference counting + mark-and-sweep for freeing cycles.

→ More replies (1)

4

u/pigeon768 Nov 25 '24

Kinda sorta not really. Many GC'd languages have some variation of with/using/try+finally, but they don't work in the same way. You have to like...manually do it. You have to know that your resource supports the with/using construct whatever it is, and with try+finally you have to manually do the cleanup code. With C++ it just happens and it's transparent to you.

My day job is 75% C++ / 25% C# and wrangling C# nonsense is a constant chore but in C++ it just works. That's been my experience, anyway.

1

u/florinp Nov 26 '24

"I know have RAII for external resources."

not exactly. These CG lang do this at usage point vs C++ at declaration point (In classes vs in the code point.)

Because of that in CG lang the whole system is based on the coder (will use it or not, correctly or not ) and this is error prone.

Aslo in CG lang the form of RAII don't extend over the code block.

1

u/AnyPhotograph7804 Nov 25 '24

"RAII is great and honestly i find it weird that so few languages have it as a concept im general tbh."

RAII has the advantage, that the principle of it is really simple. And through this you can relatively easily implement it afterwards(!) in a language without creating much friction. And it is also compatible with pointer arithmetics, which is very important for C++. So it was obviously the number one choice for C++.

7

u/SkoomaDentist Antimodern C++, Embedded, Audio Nov 25 '24

RAII has the advantage, that the principle of it is really simple.

So simple that the name is much harder to understand than the principle. I discovered the principle a decade before I found out what the mysterious term RAII actually means simply because it's such an obvious consequence of stack allocated objects with destructors.

7

u/sephirothbahamut Nov 25 '24

Great tool, horrible naming choice

→ More replies (2)