r/ProgrammerHumor • • Jan 28 '23

Meme C++

Post image
53.9k Upvotes

1.5k comments sorted by

View all comments

Show parent comments

354

u/throw3142 Jan 28 '23

The problem isn't even that modern languages are more convenient. There is a real niche for systems languages with object-oriented features. The problem is that C++ is burdened by heavy backwards-compatibility requirements, it's unsafe by design, the ecosystem is a mess (it's so hard to link dependencies, compared to literally every other language including C and even raw assembly), and there is no consensus on style because there are so many ways to do the same thing (e.g. pointer vs reference vs rvalue reference vs smart pointer, #define vs constexpr, CRTP vs inheritance, throw exception vs return error code vs set errno, explicit cast vs implicit cast, lock_guard vs scoped_lock, #ifndef vs #pragma once, .cc vs .cpp file extension, .h vs .hpp file extension).

107

u/marti_2203 Jan 28 '23

Some of these things are solutions from different periods (smart pointers and the things before it, ifndef and pragma once) but I think the inheritance and CRTP solve vastly different problems. And thank you for reminding me about .cc vs .cpp

20

u/merlinsbeers Jan 28 '23

vs .C vs .CC

No really. There were people using .c for C and .C for C++. On Windows.

3

u/marti_2203 Jan 28 '23

When did this happen? What is the history behind this???

2

u/throw3142 Jan 28 '23

lmao I had no idea. Wait aren't filenames case insensitive on Windows? That's even worse!

7

u/[deleted] Jan 28 '23

I've heard that using POSIX subsystem for Windows (introduced in WinNT 3.1) you could achieve case sensitivity on Windows.

35

u/throw3142 Jan 28 '23

Yeah I totally agree, each of these variants is different for a reason, and each one has its place. That doesn't stop them from being mixed together in weird combos and making the codebase completely unreadable lol

5

u/marti_2203 Jan 28 '23

Legacy spaghetti code sprinkled with technical debt, my favourite

1

u/[deleted] Jan 28 '23

Can you even do CRTP without inheritance? It's more like a very specific case of inheritance, utilizing the C++ template system.

2

u/marti_2203 Jan 28 '23

Yeah, but the CRTP makes it such that there is no vtable iirc and that was the big thing

3

u/Tranzistors Jan 28 '23

There are good guidelines.

pointer vs reference vs rvalue reference vs smart pointer

  • reference when the target totally exists, recommended;

  • raw pointer when the target may or may not exist;

  • I.11 smart pointer when you own the resource and it lives on the heap;

  • local variable when you own it and it lives on the stack;

  • rvalue when the data doesn't need to live longer than the statement.

#define vs constexpr

ES.31 whenever possible use constexpr

throw exception vs return error code vs set errno

First of all, figure out if you are in an environment where you are allowed to throw. But if you can.

  • I.10 usually throw (e.g. failed to construct a std::vector);

  • if it's 'normal' that the operation could not deliver desirable outcome, return std::expected (e.g. smallest_prime_between(int min, int max);

  • errno is sad (E.28), only use in legacy, preferably wrap.

explicit cast vs implicit cast

explicit where reasonably possible; implicit is where dragons live.

lock_guard vs scoped_lock

CP.21

#ifndef vs #pragma once

SF.8 #ifndef, #pragma is non-standard. Or use modules (whenever they become available).

.cc vs .cpp file extension, .h vs .hpp file extension

NL.27; .cpp + .h

CRTP vs inheritance

C.hier. Cool guys prefer static inheritance, but I dunno.

1

u/throw3142 Jan 28 '23

Thanks for the reference. I agree on all counts - the problem is that not everyone does :) Since the population of C++ devs is so varied, you have a lot of ppl who still prefer the old C-like style and a lot of newbies who just pick a random style when they don't know what to do. Often within the same codebase.

2

u/AssAsser5000 Jan 28 '23

This right here. I left cpp world to do java and I sort of feel like I wussed out and I sort of feel like I made a brilliant choice.

1

u/Kingkwon83 Jan 29 '23

Why is it unsafe by design?

1

u/throw3142 Jan 30 '23

The field of computer security is predicated on the fact that bugs are inevitable, and safety comes from 1) making bugs harder to write, and 2) mitigating the damage that can be done by an exploited bug. By "unsafe by design", I meant that there are many categories of bugs that are very easy to accidentally create in C / C++, and the amount of damage that can be done by an exploited bug is quite high.

Memory errors are the most obvious kinds of bugs that are easy to do in C++. Use-after-free, dangling pointer, memory leak, double free, etc. Most of these bugs can lead to a DoS attack in the best case, and arbitrary exfiltration of confidential info in the worst case.

Then there's pointer casting. Pointer casting is actually less safe in C++ than C, since in C++, many people assume that calling an object's constructor is the only way to build an object of that type - but you could always pointer-cast a region of memory to that type, bypassing the constructor.

In C, all functions use static dispatch, unless you explicitly use function pointers in order to run a function dynamically. In C++, there is a weird gray area between static and dynamic dispatch - it depends whether the method was declared virtual or not. This makes it very easy to make mistakes.

I could go on, but in summary, the point is that a language that makes it easy to write exploitable bugs is unsafe by design. Note that being unsafe by design isn't necessarily a bad thing - there exists a tradeoff in terms of safety vs speed.

But recently, languages like Go and Rust are pushing the envelope of the safety / speed tradeoff. They are able to approach C++ levels of speed while introducing language-level features to avoid safety issues (i.e. the garbage collector + lack of pointer casting in Go, and the borrow checker + null-less type system in Rust). They also give you the ability to use unsafe features if you really need them (i.e. the "unsafe" package in Go and unsafe blocks / functions in Rust), but the fact that these features must be explicitly enabled means that it is much easier to avoid shooting yourself in the foot.

Of course, you can still write bugs in higher-level languages like Java, Python, and Go. And these bugs can still be quite severe. But it is typically quite difficult to find something as exploitable as a memory safety bug in software written in these languages. Again, not saying it's impossible - Log4Shell was pretty bad, after all ... it's just much less common.

2

u/Kingkwon83 Jan 30 '23

Thanks for the indepth explanation!

1

u/throw3142 Jan 30 '23

Sure, glad it was helpful!