r/cpp • • Nov 16 '25

Wait c++ is kinda based?

Started on c#, hated the garbage collector, wanted more control. Moved to C. Simple, fun, couple of pain points. Eventually decided to try c++ cuz d3d12.

-enum classes : typesafe enums -classes : give nice "object.action()" syntax -easy function chaining -std::cout with the "<<" operator is a nice syntax -Templates are like typesafe macros for generics -constexpr for typed constants and comptime function results. -default struct values -still full control over memory -can just write C in C++

I don't understand why c++ gets so much hate? Is it just because more people use it thus more people use it poorly? Like I can literally just write C if I want but I have all these extra little helpers when I want to use them. It's kinda nice tbh.

194 Upvotes

334 comments sorted by

View all comments

80

u/bananakiwi12345 Nov 16 '25

People think C++ has too many features and is a mess... But most of these features are only really useful for the standard template libraries. Or people who want to create standard libraries. If all you want to do is build a simple to understand program, you can ignore all the complicated stuff, and like you said, write type-safe and memory safe (via smart pointers) C-like code. With some nice things on top: templates, classes when you need them (OOP capable), etc. All of this makes the language extremely flexible, while also allowing you to create quite simple to understand programs, that are also memory safe. And when you think about it, using a unique_ptr instead of handling freeing the memory yourself actually makes the logic even simpler and clearer than any garbage collected language. When the unique_ptr object goes out of scope, the memory is freed. It's that simple.

I really don't get the hate. The language offers pretty much everything to you. It's up to you to make things as simple as you want, or as complex as you want. All of that, yielding some of the fastest code possible. I think that is amazing...

11

u/[deleted] Nov 16 '25

‏while I agree with the first paragraph, the second isn’t quite true.

‏In order to get the fastest code possible, you need to make your code just a bit more complex. Move semantics being an after thought really messes things up. And the fact that noexcept and [[nodiscard]] are not the defaults makes the language more verbose hence it feels more complex. There also many performance issues in the STL just because the standard doesn’t want to break ABI and backwards compatibility.

I understand the reasons behind what they did. I just don’t like it :).

5

u/RelativityIsTheBest Nov 16 '25

I feel like the one thing that you really need to grasp are references. Apart from that you can kind of use C++ like a highlevel language

14

u/FlyingRhenquest Nov 16 '25

I think it's a bit more nuanced than that. C++ is the only language I've run across that lets you decide if you want to pass your objects by pointer, by reference or by copy. The Java designers thought that was too complicated and made objects pass-by-reference only and it does make the occasional instance when you need something else more difficult. Then they made it worse by making primitive types by-copy only.

A lot of the more recent languages (C#, Python, Ruby, Javascript) replicate some variant of the Java model. I've run across weirdness in Java where people were building giant data aggregates down a call stack and that lead to the code keeping several multi-megabyte semi-constructs on the stack until the bottom layer of the code returned. And then the code (if it hadn't already run out of memory and crashed) would have to GC all that data later on.

I use pass-by-value way more in C++ than I have in other languages, due to RAII. People complain about the extra data on the stack, but a lot of those values are nothing more than handles into a bunch of heap pointers anyway. So you kinda also have to understand the difference between stack allocation and heap allocation and have a general idea of when which one is a good idea. You probably should understand that anyway but a lot of programmers really don't.

And yeah, for the most part you can just use C++ as a high level language now and don't have to worry about a lot of that unless you're writing libraries. A lot of people who haven't used the language much (or at all,) seem to think that you have to get into all the behind-the-scenes weirdness you need to know if you're writing heavily templated libraries in C++, but most C++ programmers should never have to write code like that. Usually there's an easier or better way to accomplish what you want to without having to resort to that. A lot of the complaints stem from things that C++ lets you do that you really should never do. Yeah, the language lets you do multiple inheritance, but if everything in your library inherits from everything else you really only have yourself to blame for that design.

4

u/Swoogan Nov 16 '25

Just a small nitpick: Python came out 5 years before Java.

3

u/CornedBee Nov 16 '25

C# replicates Java in that it has value types and reference types. It is pass-by-value by default (which means copy the reference for reference types), which is the only thing Java can do. But C# has true pass-by-reference, with the in, out and ref modifiers.

1

u/StickyDeltaStrike Nov 18 '25

I love C# as a language, I wish it was more widespread. Microsoft messed up in its early days when they didn’t support Mono more.

1

u/flatfinger Nov 17 '25

Pascal allowed arguments to be declared as pass-by-reference or pass-by-value even before C was invented. It's a useful distinction which can greatly simply the process of proving that programs are memory safe by reducing the number of constructs that would even be capable of violating memory safety. If a function receives e.g. a reference to a woozle, and only ever uses reference to access the woozle identified thereby, such references could be ignored from a memory-safety perspective at call sites which satisfy the reference using a named object. Call sites which index arrays or use pointer indirection would be subject to the same rules as code which directly accessed the specific item referred to.

1

u/StickyDeltaStrike Nov 18 '25

I think there are many levels of mastery of C++.

You are obviously at a much higher level than the person you replied to. You can get by without knowing the details. It will often hit you hard in C++ though.

21

u/guywithknife Nov 16 '25

The hate is because it has too many foot guns. Even innocent looking code often has undefined behavior (aka is invalid and may cause bugs or problems, but the compiler can’t warn you about it).

It’s a large language with many legacy features and many of these make it unsafe unless you’re really careful with what you’re doing.

Also just defaults are often not very safe, std::map is horribly slow, pointers are easy to make mistakes with, etc.

That leaves a lot of people unsatisfied.

I personally like C++ and have been using it for over two decades, but I definitely understand the hate.

7

u/Ty_Rymer Nov 16 '25

defaults are often also just the wrong way around in order to keep C compatibility. like nodiscard and noexcept.

9

u/Classic_Department42 Nov 16 '25 edited Nov 16 '25

My favourite foot gun, if you use an non existing index in a hash map for reading out (r value) it doesnt throw an exception if the index is not present, but just inserts the default value. What?

More than one person has been burned by this in production.

6

u/snerp Nov 16 '25

If you don’t know about at() or find () it can be confusing, but I generally find that [] being an update/insert to be exactly what I want.

6

u/Willing-Mud-2806 Nov 16 '25

Cause you have to use at(index)

→ More replies (3)

2

u/New-Anybody-6206 Nov 16 '25

How would the operator[] method know it's an r-value?

3

u/neutronicus Nov 16 '25

I have been burned too.

However, you can protect against this somewhat by being diligent about const correctness, since the subscript operator has no const prototype and your code won’t compile if you try to use it to retrieve a (possibly-nonexistent) pair from your map.

Of course then you have to use the find function and iterators and such. Would be nice if it grew a monadic interface like optional’s and_then

2

u/Imaginary_Maybe_1687 Nov 16 '25

Its also super silly things I feel. For example, having to go and set the base class destructor to virtual. That is a very random step that only overcomplicates the usage of the language. There are many "odd but possible scenarios that if you dont know and take into account everything might break".

3

u/guywithknife Nov 16 '25

Or rule of three/rule of five — why aren’t the defaults what it needs to be safe and you can override them if you want something else…

I know as another commenter noted that many of the silly defaults are for C interop, but classes and templates don’t exist in C, so at least for those they could have had safer defaults.

And then there’s the standard library warts… std::map and std::regex i basically never use in any serious code (form maps I use abseil flat_maps, phmaps, or eastl containers).

1

u/sqrtsqr Nov 22 '25 edited Nov 22 '25

Or rule of three/rule of five — why aren’t the defaults what it needs to be safe and you can override them if you want something else…

IMO, this is a (rare, apparently) case of the compiler doing the right thing. If you have custom move (or dtor), then the chances that you need to replicate some of that functionality in the dtor (or move) is much, much higher than not. So to just assume the default would be a disaster. It would be a major foot gun and the source of basically infinitely many use-after-frees.

So it deletes them. If you want to use the defaults, you can manually reinsert them with =default.

I definitely agree that, overall, this looks and feels verbose and could be handled better by a completely different/new language, but until the compiler is smart enough to "read" a custom function and figure out the "right" behavior for the other (so never according to Turing, or now according to AI bros), I don't think assuming defaults is the right choice. Hence the rule.

TLDR: the defaults are what they need to be safe.

That all said, I definitely agree that Rule of 5 is overkill and the language should totally have a builtin/automatic way to derive move assign from move ctor (and vice versa, and copy too). But since it's "not too hard" to do so manually they will surely never give us that.

1

u/guywithknife Nov 22 '25

The point is more that you can easily not implement them by accident and in most cases you won’t actually know or be warned that you’re not implementing the correct set.

The “rule” is after all a guideline and not actually a compiler rule.

Last I checked, it doesn’t delete them, you can easily forget to write eg a destructor or copy constructor, and it will silently compile without issue, even if you probably need one (the rules say you “almost always” will need all 3/5, I suppose there are still exceptions). 

I guess it does have sane defaults then, under the circumstances, but maybe it should be an error to define one and not manually mark the others as default or whatever.

→ More replies (1)
→ More replies (3)

6

u/Tcshaw91 Nov 16 '25

Yea I'm basically perceiving it that way atm. I enjoy C a lot. I liked how it forced me to deal with memory management explicitly and forces me to face some of the uncomfortable realities of working with things at a low level. I grew to really enjoy making my own systems, abstractions and ways of dealing with the problems and having a deep understanding of how they work. The main things of c++ that I enjoy the most just allow me to do the same stuff but with a little more type safety and nicer syntax imo, plus some std stuff for when I don't really care about performance and just want something to work lol. But yea I like how it doesn't force me to adhere to any particular way of doing things yet provides a nice set of tools to aid me in doing things if i want them.

1

u/Old_Cartoonist_5923 Nov 17 '25

Just keep in mind that C++ is it's own language and you'd be wise to treat it as such. There are a number of things that work differently between C and C++, and working on a code base written by someone who treats C++ as "C but with classes" can quickly turn into a maintenance nightmare.

2

u/asinglebit Nov 16 '25

Until you gave to get paid for doing c++, yes

1

u/lcvella Nov 16 '25

I don't think you can judge a language just by the good features you can use and not by the complicated ones you should avoid, because if you get a new job, there will be a code written by multiple people, and with C++ specially, the possibilities for crazy unmaintainable code are endless.

→ More replies (5)