←back to thread

The C23 edition of Modern C

(gustedt.wordpress.com)
515 points bwidlar | 4 comments | | HN request time: 1.373s | source
Show context
belter ◴[] No.41850897[source]
Important reminder just in the Preface :-)

Takeaway #1: "C and C++ are different: don’t mix them, and don’t mix them up"

replies(8): >>41850960 #>>41851047 #>>41851166 #>>41851693 #>>41853183 #>>41855660 #>>41857019 #>>41858537 #
pjmlp ◴[] No.41850960[source]
Specially relevant to all those folks that insist on "Coding C with a C++ compiler", instead of safer language constructs, and standard library alternatives provided by C++ during the last decades.
replies(3): >>41851031 #>>41851082 #>>41851268 #
com2kid ◴[] No.41851082[source]
Perfectly valid to do if you need to interface with a large C code base and you just want to do some simple OO here and there. Especially if you cannot have runtime exceptions and the like.

This is how I managed to sneak C++ into an embedded C codebase. We even created some templates for data structures that supported static allocation at compile time.

replies(2): >>41851140 #>>41852515 #
f1shy ◴[] No.41851140[source]
What would be an example of "simple OO here and there" that cannot be done cleanly in plain C?
replies(6): >>41851225 #>>41851244 #>>41851407 #>>41851799 #>>41851998 #>>41854294 #
adamrezich ◴[] No.41851244[source]
Namespaces, methods.
replies(1): >>41851261 #
f1shy ◴[] No.41851261[source]
Namespaces is not object orientation, is it? Am I missing something? You can place functions (methods) inside of structs in C23, can't you?
replies(3): >>41851286 #>>41851339 #>>41851481 #
1. adamrezich ◴[] No.41851339[source]
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.

replies(1): >>41852319 #
2. uecker ◴[] No.41852319[source]
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.
replies(2): >>41853276 #>>41858696 #
3. adamrezich ◴[] No.41853276[source]
In C++ where you have methods? Sure. It would be nice to have in C, though. But, alas, ABI compatibility.
4. listeria ◴[] No.41858696[source]
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...