427
average c++ dev (programming.dev)

I don't think that casting a range of bits as some other arbitrary type "is a bug nobody sees coming".

C++ compilers also warn you that this is likely an issue and will fail to compile if configured to do so. But it will let you do it if you really want to.

That's why I love C++

top 50 comments
sorted by: hot top controversial new old
[-] LillyPip@lemmy.ca 7 points 2 days ago* (last edited 2 days ago)

Why use a strongly typed language at all, then?

Sounds unnecessarily restrictive, right? Just cast whatever as whatever and let future devs sort it out.

$myConstant = ‘15’;
$myOtherConstant = getDateTime();
$buggyShit = $myConstant + $myOtherConstant;

Fuck everyone who comes after me for the next 20 years.

[-] merc@sh.itjust.works 43 points 3 days ago

"C++ compilers also warn you..."

Ok, quick question here for people who work in C++ with other people (not personal projects). How many warnings does the code produce when it's compiled?

I've written a little bit of C++ decades ago, and since then I've worked alongside devs who worked on C++ projects. I've never seen a codebase that didn't produce hundreds if not thousands of lines of warnings when compiling.

[-] Ajen@sh.itjust.works 6 points 2 days ago

My team uses the -Werror flag, so our code won't compile if there are any warnings at all.

[-] Croquette@sh.itjust.works 6 points 2 days ago

A production code should never have any warning left. This is a simple rule that will save a lot of headaches.

[-] phoenixz@lemmy.ca 4 points 2 days ago

Neither should your development code, except for the part where you're working on.

[-] Zacryon@feddit.org 26 points 3 days ago

I mostly see warnings when compiling source code of other projects. If you get a warning as a dev, it's your responsibility to deal with it. But also your risk, if you don't. I made it a habit to fix every warning in my own projects. For prototyping I might ignore them temporarily. Some types of warnings are unavoidable sometimes.

If you want to make yourself not ignore warnings, you can compile with -Werror if using GCC/G++ to make the compiler a pedantic asshole that doesn't compile until you fix every fucking warning. Not advisable for drafting code, but definitely if you want to ship it.

load more comments (1 replies)
[-] Jankatarch@lemmy.world 19 points 3 days ago

I put -Werror at the end of my makefile cflags so it actually treats warnings as errors now.

[-] jkercher@programming.dev 19 points 3 days ago

You shouldn't have any warnings. They can be totally benign, but when you get used to seeing warnings, you will not see the one that does matter.

load more comments (1 replies)
[-] nroth@lemmy.world 13 points 3 days ago

0 in our case, but we are pretty strict. Same at the first place I worked too. Big tech companies.

[-] dejected_warp_core@lemmy.world 10 points 3 days ago

Ideally? Zero. I'm sure some teams require "warnings as errors" as a compiler setting for all work to pass muster.

In reality, there's going to be odd corner-cases where some non-type-safe stuff is needed, which will make your compiler unhappy. I've seen this a bunch in 3rd party library headers, sadly. So it ultimately doesn't matter how good my code is.

There's also a shedload of legacy things going on a lot of the time, like having to just let all warnings through because of the handful of places that will never be warning free. IMO its a way better practice to turn a warning off for a specific line.. Sad thing is, it's newer than C++ itself and is implementation dependent, so it probably doesn't get used as much.

load more comments (2 replies)
load more comments (7 replies)
[-] deegeese@sopuli.xyz 130 points 4 days ago

C lets you shoot yourself in the foot.

C++ lets you reuse the bullet.

[-] mindbleach@sh.itjust.works 38 points 4 days ago

C is dangerous like your uncle who drinks and smokes. Y’wanna make a weedwhacker-powered skateboard? Bitchin’! Nail that fucker on there good, she’ll be right. Get a bunch of C folks together and they’ll avoid all the stupid easy ways to kill somebody, in service to building something properly dangerous. They’ll raise the stakes from “accident” to “disaster.” Whether or not it works, it’s gonna blow people away.

C++ is dangerous like a quiet librarian who knows exactly which forbidden tomes you’re looking for. He and his… associates… will gladly share all the dark magic you know how to ask about. They’ll assure you that the power cosmic would never, without sufficient warning, pull someone inside-out. They don’t question why a loving god would allow the powers you crave. They will show you which runes to carve, and then, they will hand you the knife.

[-] Contramuffin@lemmy.world 93 points 4 days ago

What do you mean I'm not supposed to add 0x5f3759df to a float casted as a long, bitshifted right by 1?

[-] panda_abyss@lemmy.ca 89 points 4 days ago

I actually do like that C/C++ let you do this stuff.

Sometimes it's nice to acknowledge that I'm writing software for a computer and it's all just bytes. Sometimes I don't really want to wrestle with the ivory tower of abstract type theory mixed with vague compiler errors, I just want to allocate a block of memory and apply a minimal set rules on top.

load more comments (31 replies)
[-] magic_lobster_party@fedia.io 55 points 3 days ago

There are no medals waiting for you by writing overly clever code. Trust me, I’ve tried. There’s no pride. Only pain.

[-] Ajen@sh.itjust.works 4 points 2 days ago

Debugging code is always harder that writing it in the first place. If you make it as clever as you can, you won't be clever enough to debug it.

[-] magic_lobster_party@fedia.io 2 points 2 days ago

Some junior will call it ”skill issues” and then write the most unreadable code ever.

[-] Chrobin@discuss.tchncs.de 25 points 3 days ago

It really depends on your field. I'm doing my master's thesis in HPC, and there, clever programming is really worth it.

load more comments (3 replies)
load more comments (5 replies)
[-] Gobbel2000@programming.dev 22 points 3 days ago

I'm all for having the ability to do these shenanigans in principle, but prefer if they are guarded in an unsafe block.

[-] Opisek@lemmy.world 2 points 2 days ago* (last edited 2 days ago)

The problem is that it's undefined behavior. Quake fast inverse square root only works before the types just happen to look that way. Because the floats just happens to have that bit arrangement. It could look very different on other machines! Nevermind that it's essentially always exactly the same on most architectures. So yeah. Undefined behavior is there to keep your code usable even if our assumptions about types and memory change completely one day.

[-] BigDanishGuy@sh.itjust.works 25 points 3 days ago

But it will let you do it if you really want to.

Now, I've seen this a couple of times in this post. The idea that the compiler will let you do anything is so bizarre to me. It's not a matter of being allowed by the software to do anything. The software will do what you goddamn tell it to do, or it gets replaced.

WE'RE the humans, we're not asking some silicon diodes for permission. What the actual fuck?!? We created the fucking thing to do our bidding, and now we're all oh pwueez mr computer sir, may I have another ADC EAX, R13? FUCK THAT! Either the computer performs like the tool it is, or it goes the way of broken hammers and lawnmowers!

[-] mormegil@programming.dev 7 points 2 days ago

I understand the idea. But many people have hugely mistaken beliefs about what the C[++] languages are and how they work. When you write ADC EAX, R13 in assembly, that's it. But C is not a "portable assembler"! It has its own complicated logic. You might think that by writing ++i, you are writing just some INC [i] ot whatnot. You are not. To make a silly example, writing int i=INT_MAX; ++i; you are not telling the compiler to produce INT_MIN. You are just telling it complete nonsense. And it would be better if the compiler "prevented" you from doing it, forcing you to explain yourself better.

[-] BigDanishGuy@sh.itjust.works 3 points 2 days ago

I get what you're saying. I guess what I'm yelling at the clouds about is the common discourse more than anything else.

If a screw has a slotted head, and your screwdriver is a torx, few people would say that the screwdriver won't allow them to do something.

Computers are just tools, and we're the ones who created them. We shouldn't be submissive, we should acknowledge that we have taken the wrong approach at solving something and do it a different way. Just like I would bitch about never having the correct screwdriver handy, and then go look for the right one.

[-] CanadaPlus@lemmy.sdf.org 3 points 2 days ago* (last edited 1 day ago)

Yeah, but there's some things computers are genuinely better at than humans, which is why we code in the first place. I totally agree that you shouldn't ever be completely controlled by your machine, but strong nudging saves a lot of trouble.

[-] BuboScandiacus@mander.xyz 19 points 3 days ago

Ok gramps now take your meds and off you go to the retirement home

load more comments (1 replies)
[-] AnyOldName3@lemmy.world 11 points 3 days ago

Soldiers are supposed to question potentially-illegal orders and refuse to execute them if their commanding officer can't give a good reason why they're justified. Being in charge doesn't mean you're infallible, and there are plenty of mistakes programmers make that the compiler can detect.

load more comments (1 replies)
[-] daniskarma@lemmy.dbzer0.com 12 points 3 days ago

I will botton for my rust compiler, I'm not going to argue with it.

[-] pipe01@programming.dev 1 points 2 days ago

New copypasta just dropped

load more comments (3 replies)
[-] eager_eagle@lemmy.world 58 points 4 days ago

C++: all the footguns you need plus a lot more that you never imagined in a single language

[-] UnfortunateShort@lemmy.world 26 points 3 days ago* (last edited 3 days ago)

I used to love C++ until I learned Rust. Now I think it is obnoxious, because even if you write modern C++, without raw pointers, casting and the like, you will be constantly questioning whether you do stuff right. The spec is just way too complicated at this point and it can only get worse, unless they choose to break backwards compatibility and throw out the pre C++11 bullshit

[-] mobotsar@sh.itjust.works 15 points 3 days ago* (last edited 7 hours ago)

Depending on what I'm doing, sometimes rust will annoy me just as much. Often I'm doing something I know is definitely right, but I have to go through so much ceremony to get it to work in rust. The most commonly annoying example I can think of is trying to mutably borrow two distinct fields of a struct at the same time. You can't do it. It's the worst.

load more comments (2 replies)
[-] LovableSidekick@lemmy.world 12 points 3 days ago

As it should be. Airbags should go off when you crash, not when you drive near the edge of a cliff.

[-] Treczoks@lemmy.world 19 points 3 days ago

Structs with union members that allow the same place in memory to be accessed either word-wise, byte-wise, or even bit-wise are a god-sent for everyone who needs to access IO-spaces, and I'm happy my C-compiler lets me do it.

load more comments (7 replies)
[-] pennomi@lemmy.world 37 points 4 days ago

C++ is kinky that way. You can consent to all manner of depraved programming patterns. Great for use in personal life, but maybe not appropriate for the office.

[-] Korne127@lemmy.world 53 points 4 days ago

But does it have cargo-mommy :P

load more comments (3 replies)
[-] wer2@lemmy.zip 32 points 4 days ago

My issue is C++ will "let me do it", and by that I mean "you didn't cast here (which is UB), so I will optimize out a null check later, and then segfault in a random location"

load more comments (1 replies)
[-] Shanmugha@lemmy.world 2 points 2 days ago

Aand what is wrong with that?

[-] psycho_driver@lemmy.world 16 points 3 days ago

I don't know which is worse. Using C++ like lazy C, or using C++ like it was designed to be used.

load more comments (1 replies)
load more comments
view more: next ›
this post was submitted on 22 Jul 2025
427 points (98.2% liked)

Programmer Humor

25253 readers
1435 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS