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++

    • merc@sh.itjust.works
      link
      fedilink
      arrow-up
      4
      ·
      5 months ago

      Not only that, but everyone who sees that code later is going to waste so much time trying to understand it. That includes future you.

    • Ajen@sh.itjust.works
      link
      fedilink
      arrow-up
      3
      ·
      5 months 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.

  • merc@sh.itjust.works
    link
    fedilink
    arrow-up
    26
    ·
    5 months 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.

    • jkercher@programming.dev
      link
      fedilink
      English
      arrow-up
      13
      ·
      5 months 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.

    • nroth@lemmy.world
      link
      fedilink
      arrow-up
      9
      ·
      5 months 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
      link
      fedilink
      arrow-up
      6
      ·
      5 months 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.

      • merc@sh.itjust.works
        link
        fedilink
        arrow-up
        3
        ·
        5 months ago

        I’ve seen this a bunch in 3rd party library headers, sadly. So it ultimately doesn’t matter how good my code is.

        Yeah, I’ve seen that too. The problem is that once the library starts spitting out warnings it’s hard to spot your own warnings.

    • vivendi@programming.dev
      link
      fedilink
      English
      arrow-up
      4
      ·
      5 months ago

      Ignoring warnings is really not a good way to deal with it. If a compiler is bitching about something there is a reason to.

      A lot of times the devs are too overworked or a little underloaded in the supply of fucks to give, so they ignore them.

      In some really high quality codebases, they turn on “treat warnings as errors” to ensure better code.

      • merc@sh.itjust.works
        link
        fedilink
        arrow-up
        2
        ·
        5 months ago

        I know that should be the philosophy, but is it? In my experience it seems to be normal to ignore warnings.

    • Ajen@sh.itjust.works
      link
      fedilink
      arrow-up
      4
      ·
      5 months ago

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

  • Pennomi@lemmy.world
    link
    fedilink
    English
    arrow-up
    22
    ·
    5 months 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.

  • BigDanishGuy@sh.itjust.works
    link
    fedilink
    arrow-up
    21
    ·
    5 months 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!

    • WhyJiffie@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      8
      ·
      5 months ago

      when life gives you restrictive compilers, don’t request permission from them! make life take the compilers back! Get mad! I don’t want your damn restrictive compilers, what the hell am I supposed to do with these? Demand to see life’s manager! Make life rue the day it thought it could give BigDanishGuy restrictive compilers! Do you know who I am? I’m the man who’s gonna burn your house down! With the compilers! I’m gonna get my engineers to invent a combustible compiler that burns your house down!

    • AnyOldName3@lemmy.world
      link
      fedilink
      arrow-up
      8
      ·
      5 months 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.

      • BigDanishGuy@sh.itjust.works
        link
        fedilink
        arrow-up
        3
        ·
        5 months ago

        I get the analogy, but I don’t think that it’s valid. Soldiers are, much to the chagrin of their commanders, sentient beings, and should question potentially illegal orders.

        Where the analogy doesn’t hold is, besides my computer not being sentient, what I’m prevented from doing isn’t against the law of man.

        I’m not claiming to be infallible. After all to err is human, and I’m indeed very human. But throw me a warning when I do something that goes against best practices, that’s fine. Whether I deal with it is something for me to decide. But stopping me from doing what I’m trying to do, because it’s potentially problematic? GTFO with that kinda BS.

    • mormegil@programming.dev
      link
      fedilink
      arrow-up
      5
      ·
      5 months 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
        link
        fedilink
        arrow-up
        3
        ·
        5 months 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.

  • UnfortunateShort@lemmy.world
    link
    fedilink
    English
    arrow-up
    19
    ·
    edit-2
    5 months 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
      link
      fedilink
      English
      arrow-up
      11
      ·
      edit-2
      5 months 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.

  • Treczoks@lemmy.world
    link
    fedilink
    arrow-up
    15
    ·
    5 months 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.

  • Gobbel2000@programming.dev
    link
    fedilink
    arrow-up
    14
    ·
    5 months 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
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    5 months 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.

  • panda_abyss@lemmy.ca
    link
    fedilink
    arrow-up
    0
    ·
    5 months 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.

    • jkercher@programming.dev
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      4 months ago

      100%. In my opinion, the whole “build your program around your model of the world” mantra has caused more harm than good. Lots of “best practices” seem to be accepted without any quantitative measurement to prove it’s actually better. I want to think it’s just the growing pains of a young field.