r/cpp • • Aug 25 '21

Experience C++ developers: do you enjoy using C++ (even for personal/hobby projects?)

Do you actually enjoy using C++, or do you only use it for work, or purely for performance? This question is aimed at people with years of experience, who've worked on large projects in C++. I see people who dislike the language, but use it purely for performance reasons. This sort of discourages me from learning it. I don't want programming to feel like work.

204 Upvotes

327 comments sorted by

View all comments

134

u/[deleted] Aug 25 '21

"Modern C++" is much different than what a lot of students encounter in academia. I've heard some stories of the "best practices" from university courses and it sends shivers down my spine (`using namespace` everywhere, not using smart pointers, avoiding standard library, C-style structs only, etc.).

I really like C++ for the ludicrous amounts of control it gives you, and it's great for having enough high-level features to interface directly with C code without writing or getting bindings for things. A lot of the work gets front-loaded into setting up a build, so I just made up my own "starter" project for things. The simplicity of using C code and intrinsics and massive ecosystem improvements in Ada means I've been using that a lot lately, though I've used Rust quite a bit in the past as well.

I don't think you could go wrong using C++, as long as you're using updated references to learn from. Yes, the language is complicated and it seems overwhelming, but the overall system has improved immensely and you'll be learning a lot of neat low-level things.

71

u/sephirothbahamut Aug 25 '21

not using smart pointers

It's not that they say "using smart pointers is bad practice", more like they don't even know smart pointers exist.

6

u/[deleted] Aug 25 '21

Yeah, that's what I mean. I'm going to clarify in a more direct comment about this.

5

u/Real_Rutabaga Aug 25 '21

What's a smart pointer? /s All of the things people describe about not liking working with C++ is my job. Legacy code bases can be a chore.

10

u/matekelemen Aug 25 '21

They're teaching the basics first (new) so students will understand what smart pointers are meant to solve. It seems counterintuitive that advanced courses cover stuff that are easier to use, but honestly, I'm glad I was forced to learn the background first (starting from C with classes) and not the other way around.

21

u/sephirothbahamut Aug 25 '21

They're teaching the basics first

You're implying that they teach the rest later, which, at least in my experience, is false. Nobody here is saying you shouldn't learn new. The problem is they don't even mention smart pointers exist at all.

Heck, I literally had to teach my own teachers what move semantics are.

13

u/Shok3001 Aug 25 '21

In my experience most professors stopped learning C++ in 1999

5

u/maskull Aug 26 '21

It's because that's when the textbooks we're last revised. Maybe there's an appendix on smart pointers (not an appendix in the physical book, of course; it's only available online).

11

u/[deleted] Aug 25 '21

[removed] — view removed comment

26

u/[deleted] Aug 25 '21 edited Aug 25 '21

Do universities teach those as “best practices”?

What I mean is, that's the style they teach and require in class.

I'm one of the few C++ developers I know and constantly have to deal with "lmao, you suck because you write in C++, it's so bad, because I tried it in college and it was horrible." Not all professors, but the many times I've looked up what was taught at various universities of people I've talked to and found they're teaching C++98. Usually it ends up looking like forcing students to write in "C with function overloading", though I've also seen almost literal "C with classes" before. I know C++ used to be called "C with classes" but it's a lot more than that now. They ignore smart pointers since in C++ "you have to manage memory" and use using namespace std to get rid of all of "those pesky std::" to make things fit on PowerPoint slides.

They're just often not up to date and often just trying to "simplify" things for the students due to time constraints and the number of students in the class (50-100+) to ease TA and Professor burden. However, it's the programming equivalent of teaching a future mechanic about modern car design by working on a Model T.

The comparison I jokingly give in terms of similar timelines is: "Oh, you must hate Python too, because I only used version 1.5 and I didn't like it."

8

u/gnosnivek Aug 25 '21

You also just get some plain weird stuff because people don't know better and bad advice gets passed down. I remember being told in my first C++ class that any data allocated with new had to be released with free().

I mean, I guess it technically didn't break anything, but hoo boy am I glad I don't have to abide by that rule anymore.

3

u/Ahajha1177 Aug 25 '21

Oh geez, pretty sure that's UB, isn't it? Not to mention that the destructors don't get called... shudders

2

u/gnosnivek Aug 26 '21

I'm actually not 100% certain how illegal it is. It's UB to call delete on anything not allocated with new, and it's UB to call free on anything not allocated with malloc, but AFAIK there's no rule that says that a pointer allocated with new must have delete called on it (after all, leaking memory isn't UB). So if the implementation of new gets memory from malloc() and doesn't alter it in any way, this might actually be legal on that implementation. I'm not sure (would love a pointer if there's a rule that explicitly makes this undefined!)

That being said, making a habit of this seems like a surefire way to get smacked by horrifying amounts of UB at some point.

Not to mention that the destructors don't get called

Well obviously the solution to that is to manually reimplement the destructor using loops/recursion/free whenever you need to call delete. (We had to do this and it was dumb).

→ More replies (1)

9

u/gnosnivek Aug 25 '21

> why not just use C?

This is actually a really interesting question that I haven't thought about. I wonder if schools feel like they have to give both a low-level and high-level introductory programming experience, don't want to have one class for each, and fall back to C++ because you "can" do both in the same language.

I remember trying to tutor a student who was taking a C++ intro programming class, and after going through classes, constructors, inheritance, operator overloading, and some basic data structures, the class suddenly went "now for your final project, write a game of life simulation using only C constructs (arrays, malloc/free, printf)"

It was a gratuitous turn into "low level" programming (low level in massive air quotes here) and I wonder if that was there because someone said "well we need to make sure the students have experience with C before moving on" and nobody was willing to commit to actually having a quarter to properly teach it.

6

u/[deleted] Aug 25 '21

[removed] — view removed comment

2

u/SirClueless Aug 25 '21

I think it can work. At my university, they teach a freshman course aimed at future CS majors in Java, and in your sophomore year you have two courses you can take in either order taught in Java (Algorithms) and C and Assembly (Programming Systems) respectively.

It works out pretty well, I think. I firmly believe that anyone looking to be a software engineer should take a class taught in C at some point in their life, but it doesn't have to be your introduction to programming.

2

u/jk-jeon Aug 26 '21

Here is my perspective.

I started programming with VB6 when I was 11 yo and it was a lot of fun. I learned C after that and eventually abandoned VB6 in a few years because it looked like everyone was regarding VB6 as "a children's toy". But when I first learned C, the console and CUI thing looked boring and pointless since I could make more decent-looking GUI's with VB6.

My point is that, it really depends on the audience. If "the first course" is for children of age 4-9, do you think C will be more effective than something like Scratch? Or, how about the first course in university for students not majoring in CS?

Another point is this. For me, even after learning C, computer was still a total mystery until I learned a lot more details later. I mean, what the hell is monitor and keyboard, and how is it relevant to this pointer and memory thing? You know, monitor, keyboard, speaker, mouse, or whatever UI devices (and possibly including HDD/SSD?) are how ordinary people perceive computer. They don't understand why those are called "peripheral" devices. There is still too deep gap between their daily understanding of computer and what basic knowledge in C teaches them. So teaching C might not be that "touching". At least it really wasn't to me.

→ More replies (3)

7

u/da2Pakaveli Aug 25 '21

Not Uni but my school did. Some of my teachers would probably be surprised if they heard C++ had newer versions.

4

u/braxtons12 Aug 25 '21

When I took my data structures and algorithms course 6 years ago (the only course we had in c++, everything else was java, python, or C; mainly Java), the standard library was barely even acknowledged to exist, and by that I mean we were told that std::string and std::vector are a thing, and that's about it.

This was also the first course that wasn't in Java, so naturally people didn't get the whole headers vs implementations thing. And I'm not entirely convinced all the professors did either, because one never explained them at all and just had his students write everything in one huge .cpp file for each project.

→ More replies (3)

5

u/ShakaUVM i+++ ++i+i[arr] Aug 25 '21

"Modern C++" is much different than what a lot of students encounter in academia. I've heard some stories of the "best practices" from university courses and it sends shivers down my spine (`using namespace` everywhere, not using smart pointers, avoiding standard library, C-style structs only, etc.).

Using namespace std is fine in implementation files. The C++ Core Guidelines even say this. It's far more common in academia for professors to rail against it than to use it.

Smart pointers are kind of a mixed bag. There's nothing inherently wrong with a non-owning raw pointer, and you kinda have to learn how raw pointers work for the syntax of iterators and smart pointers to make sense.

But if they're using raw pointers that own, outside of maybe a RAII context, then yeah they're bad. A lot of professors learned C and C++ in the 80s and haven't updated.

C style structs are okay in some contexts, but yeah it's often a matter of their knowledge being limited.

It's hard hiring good professors in CS. Like. Ridiculously hard.

I don't think you could go wrong using C++, as long as you're using updated references to learn from. Yes, the language is complicated and it seems overwhelming, but the overall system has improved immensely and you'll be learning a lot of neat low-level things.

Yeah, if you stick to modern C++ most of the rough edges go away.

4

u/[deleted] Aug 25 '21

What's wrong with using namespace??

28

u/sephirothbahamut Aug 25 '21

In header files just don't do it.

In source files it may be acceptable.

In an enclosed scope it's totally fine.

→ More replies (2)

18

u/_JJCUBER_ Aug 25 '21

It clutters everything after it with everything from the namespace and can lead to naming collisions. Also, if you put it at the top of your file, I’m pretty sure that including it will lead to the using namespace directive being included everywhere else. There are times when you might want to use it, like maybe in a function, but most of the time, you should opt to typing out the namespace or creating a namespace alias.

0

u/[deleted] Aug 25 '21

[deleted]

21

u/ronchaine Embedded/Middleware/WG21 Aug 25 '21

There are some namespaces that are meant to be pulled, like std::chrono_literals, but that is pretty rare.

→ More replies (1)
→ More replies (1)
→ More replies (30)
→ More replies (1)
→ More replies (18)