Skip to content
DevMeme
4003 of 7590
Languages Post #4359 · source on Telegram

The C++ Type System's Deceptive Disguise

Description

This meme uses the multi-panel 'Man Ray and Patrick's Wallet' format from SpongeBob SquarePants to humorously illustrate a notorious C++ programming pitfall. In the comic, Man Ray (representing a developer) is suspicious of the `std::uint8_t` type, which Patrick (representing the C++ type system) insists is just an 'eight-bit unsigned integer.' Man Ray explicitly asks, 'You're sure this isn't a char in disguise?' and Patrick confirms, 'Yep.' The developer then proceeds to declare a variable `std::uint8_t u = 0x45;`, which is the hexadecimal value for the integer 69. However, in the final panels, when the developer tries to print the variable to the console using `std::cout << u;`, the output is 'E'. The joke, which is deeply relatable to systems programmers, is that on most C++ compilers, `std::uint8_t` is a typedef for `unsigned char`. The `std::cout` stream has an operator overload that treats `char` types as characters to be printed, not integers. Therefore, instead of printing the number 69, it prints the ASCII character corresponding to the hex value 45, which is the capital letter 'E', revealing the type's deceptive nature

Comments

22
Anonymous ★ Top Pick In C++, `uint8_t` is that one friend who insists they're just a number, but as soon as they go out, they act like a total character
  1. Anonymous ★ Top Pick

    In C++, `uint8_t` is that one friend who insists they're just a number, but as soon as they go out, they act like a total character

  2. Anonymous

    uint8_t is C++’s way of reminding you that strong typing is only a compile-time suggestion - pipe it into std::cout and your packet length field starts reciting ASCII poetry

  3. Anonymous

    After 20 years of C++, you'd think we'd have learned that the real type system is whatever the template instantiation decides it is today

  4. Anonymous

    uint8_t passed the interview as an integer, then on day one operator<< checked its references and found out it's been a char the whole time

  5. Anonymous

    Ah yes, uint8_t - the type that promises you an integer but delivers a char, because apparently the C++ standards committee thought 'let's make debugging I/O operations a delightful surprise.' You want to print 69? Here's 'E' instead. It's not a bug, it's a feature - specifically, a feature that's been gaslighting developers since C++11 standardized <cstdint>. Pro tip: cast to int before streaming, or enjoy explaining to your team why your telemetry dashboard is displaying ASCII art instead of sensor readings

  6. Anonymous

    Strong typing until the standard library shows up: uint8_t passes review as an integer, then operator<< ships an ASCII E to prod

  7. Anonymous

    uint8_t: integer until std::cout observes it, collapsing to char like a type-based quantum bit-flip

  8. Anonymous

    uint8_t: C++’s undercover unsigned char - numeric until operator<<, then it ships as ASCII E

  9. @na_weka 4y

    Explain please

    1. @neizvestnyi 4y

      "high-level" c++ language treats an explicitly typed integer as char-code in supposed object-oriented output library

      1. @na_weka 4y

        Спасибо!

        1. @lord_asmo 4y

          Man wtf is this chars

          1. dev_meme 4y

            It's danke on russian

          2. @sylfn 4y

            = thanks

          3. @SamsonovAnton 4y

            This is that you get when sending UINT8_C(0x45) to an overloaded output stream. Bwa-ha-ha!

    2. @nkormakov 4y

      in short, when you push something to stdout without formatting, it will try to print it as a char - check ASCII table in google

      1. @na_weka 4y

        Thanks!

  10. @brbrmensch 4y

    std::byte

  11. @Vlasoov 4y

    That's because std:: ints are usually typedefs, and uchar is printed just like signed one

  12. @feskow 4y

    'murica moment

  13. @ZgGPuo8dZef58K6hxxGVj3Z2 4y

    😂😂😂😂😂😂😂

  14. @lord_asmo 4y

    Нет 🧐

Use J and K for navigation