← Back to context

Comment by adamrezich

20 hours ago

Correct, and you did ask specifically for OO things, but I thought I'd list namespaces too as far as “C++ things you might use when writing C-like C++ code”.

Another big one that I always forget C still doesn't support is function overloading.

Function overloading is a feature that makes code less self-documenting without providing any meaningful value. Operator overloading is more interesting, because you can build you domain language with nice syntax. But I also tend to think that this is not really worth it.

  • Function overloading enables the building of powerful idioms like swap, operator== and this [1] proposal for composable hashing to name a few. And when combined with templates (and ADL to provide some encapsulation with namespaces) you can build some interesting abstractions akin to the io module in go, like this implementation of io.ReadFull():

        template<typename R>
        constexpr ssize_t
        io::ReadFull(R reader, char *buf, size_t len)
        {
          ssize_t recvd = 0, ret;
    
          do {
            ret = read(reader, &buf[recvd], len - recvd);
            if (ret > 0)
              recvd += ret;
            else
              break;
          } while (recvd < len);
    
          return recvd;
        }
    

    ---

    1: https://open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3980.h...

  • In C++ where you have methods? Sure. It would be nice to have in C, though. But, alas, ABI compatibility.