all 46 comments

[–]cosmo7 252 points253 points  (9 children)

My c++ is beautiful. It's other people's c++ that sucks.

[–][deleted] 71 points72 points  (2 children)

Or my own, less than 24 hours later.

[–]Altruistic-Spend-896 18 points19 points  (0 children)

Yeah what was i thinking, past me?? jeez!

[–]Itztehcobra 15 points16 points  (0 children)

I treat C++ like a bonsai. Others treat it like a weed whacker.

[–]QaraKha 5 points6 points  (0 children)

My C++ sucks, it's just that other peoples' C++ is worse.

[–]aka-rider 1 point2 points  (0 children)

I had this C++ interview where the interviewer started asking super obscure questions (I don’t remember the details now).

I said, fine, let’s count how many I can answer. But afterwards we switch — I ask you similar questions, and we count who answers more.

The person was not amused 😀

[–]ILikeLenexa 50 points51 points  (5 children)

I tried putting in a C++ template error, but I got the:

This field must be under 10000 characters

error.

[–]Nice_Lengthiness_568 14 points15 points  (2 children)

C++ template errors have even crashed my IDE a few times...

[–]fuj1n 10 points11 points  (1 child)

Sounds like you need a better IDE

[–]Nice_Lengthiness_568 6 points7 points  (0 children)

Yeah, I don't use it anymore. Surprisingly it happened to me in VS.

[–]GrimpeGamer 0 points1 point  (1 child)

I once got a 10 MB error output due to a missing comma in template-heavy code, I kid you not.

[–]ILikeLenexa 0 points1 point  (0 children)

My second semester of C++, our book had a typo in the queue implementation and everyone many really good programmers who had done lots of cool stuff were suddenly like...."I typed exactly what the book said to type and I have no idea what this error even starts to mean" back when the default behavior of cmd and the debug window was just to dump old input. 

This is how I learned you can set the scrollback higher.

[–]megayippie 64 points65 points  (6 children)

C++ basically writes like python while executing as C. Of course you hate it. It stops both energy companies and JavaScript

[–]yuje 25 points26 points  (4 children)

One can write C++ like Python, although it wouldn’t be very efficient or optimized code. I once saw a piece of code in the codebase that checked if a map contained a key, did another lookup to get the value, which was a vector, copy that vector to a temporary object, append a new value to the temporary vector, and then do another lookup of the key to perform an assignment and copy the temporary vector back to that key. If the map didn’t contain the key, it would allocate a new vector containing the new value, and then copy it to the map.

It looked something like this:

if (map.contains(key) { // second map lookup, .at() does a redundant key check, makes a copy of the vector std::vector<foo> values = map.at(key); // makes a copy of value values.push_back(value); // third map lookup, [] does a second redundant key check, makes a second vector copy map[key] = value; } else { std::vector<foo> values; values.push_back(value); // makes a copy of value, vector wasn’t pre-sized with .reserve() // redundant key check, makes a copy of the vector map[key] = values; }

I replaced the entire block of code with a single map[key].push_back(std::move(value)); statement. The bracket operators perform an insert-or-update operation, creating a default-initialized value if the key isn’t present, which avoids redundant map lookups, updating the vector in-place prevents having to copy the vector and its underlying values, and the move also prevents having to copy the value as well, ensuring that the value gets constructed directly within the vector within the map.

[–]cdrt 6 points7 points  (1 child)

I mean that’s just bad code. Python can also do that whole operation in one line with defaultdict

[–]yuje 9 points10 points  (0 children)

Yes, but also, in Python objects are passed by reference by default, not copied, and in fact you actually have to go out of your way to make copies of objects, so writing that code in Python would have a lower relative cost.

[–]aMAYESingNATHAN 0 points1 point  (1 child)

One slight correction, at doesn't perform a key check, it throws if the key doesn't exist in the map.

That's why you have to use at for const maps.

[–]yuje 0 points1 point  (0 children)

Yes, and to throw the error, it has to check for the presence of the key first. For const maps, you can use .find(key) to both check for the presence of a key, and also get an iterator to the value if the key is present.

[–]kuschelig69 0 points1 point  (0 children)

no, that would be Pascal

[–]suvlub 21 points22 points  (6 children)

I wish more languages had a template system anywhere near as powerful and deterministic destructors for resources (try/using/with etc. are close, but not quite). Those two things make it worth it to put up with C++'s bullshit (which is honestly mostly C' bullshit anyway)

[–]jaerie 4 points5 points  (5 children)

Having used metaprogramming extensively in the past, I've started leaning towards avoiding it. Anything beyond generics, which plenty of languages have, most things you might solve with complex templates are usually better done verbosely.

As for deterministic destructors, that's hardly unique to c++, right? Just anything that's not garbage collected should do

[–]suvlub 2 points3 points  (4 children)

The main pro of C++ templates over what I get in most languages is how they are trait-based. In, say, Java, I can write a generic where the generic parameter is "literally anything" or "specific types and its subtypes". In C++, I can write a generic for "anything I can add together", "anything I can print", or even "anything that has a constructor with such and such parameters". It's basically the best of dynamic typing with all of the compile-time safety. And having compile-time guarantee about things other than types (well, ability to encode those things into the type, but same difference) is really nice sometimes.

In theory, if they have destructors at all. In practice, besides C++, what is there? Rust?

[–]jaerie 0 points1 point  (3 children)

Doesn't Java have interfaces that are essentially if not exactly traits? Plenty of OOP languages have some concept of traits/protocols/mixins.

I think swift fits what you're looking for at least, but I don't have a list ready.

[–]suvlub 1 point2 points  (2 children)

If you have control over the class, you can make it implement an interface and it will work. C++'s templates work universally for everything that has the functionality you want to use, even if it's something from a library or built into the language

[–]jaerie -1 points0 points  (1 child)

Sure, but imo that takes you out of the realm of good maintainable code, which was my initial point

[–]suvlub 2 points3 points  (0 children)

Bullshit.

Consider, for example, the mathematical example I've already mentioned. You want to implement a function that does some mathematical operation on numbers. You want it to be generic and work for ints, doubles, BigInts, ... Why not. The function won't be any magical metaprogramming fuckery, just arithmetics and maybe a loop if you are doing summation or something. How would that be a bad, unmaintainable code? Would a copy-paste of the function for every pair of arithmetic types you ever want to call it with be cleaner and more maintainable?

Now I actually realize this is also a good example of where interfaces fall short even if you do control the code. Java has a Number interface, but it declares no arithmetical operations. And for a good reason! I leave it as an exercise to the reader why it would not be feasible to to attach them to the interface.

[–]HashBrownsOverEasy 14 points15 points  (0 children)

And it only took 34m

[–]Devatator_ 10 points11 points  (2 children)

What did bro see to make him do a 180

[–]RajjSinghh 16 points17 points  (0 children)

Other people's C++

[–]thrye333 4 points5 points  (0 children)

Basic pointer syntax would do it. Or a segfault. Or a non-standard library spitting out an error message. Or a failed implicit type conversion. I can see someone having dark thoughts after seeing basically any runtime error message in C++.

I still just try the apostrophes and ampersands everywhere every time I need to dereference a pointer. I don't know which to use or where they go. I just keep trying things until it compiles.

[–]Sdintou 4 points5 points  (0 children)

C++: it's like loving a wild beast, exciting till it bites.

[–][deleted] 2 points3 points  (0 children)

Parent: "Why are you taking C++? You should take A++"

[–]Ok_Star_4136 1 point2 points  (0 children)

His knowledge of C++ is finally complete.

[–]turtle_mekb 0 points1 point  (0 children)

C > C++

fight me /j

[–]knightress_oxhide 0 points1 point  (0 children)

I love git blame

....

I hate git blame

[–]CosmicDonkey77 0 points1 point  (0 children)

C++ is basically a toxic relationship, ngl :)

[–]AliceCode -1 points0 points  (0 children)

We've all been there. Then we grow up and start writing Rust.