News for hackers. Stuff that matters.
Re:The C23 edition of Modern C is now available for free by adamrezich on Tuesday October 15, 2024 @02:02PM (#41851244)
Namespaces, methods.
Re:The C23 edition of Modern C is now available for free by f1shy on Tuesday October 15, 2024 @02:04PM (#41851261)
Namespaces is not object orientation, is it? Am I missing something? You can place functions (methods) inside of structs in C23, can't you?
Re:The C23 edition of Modern C is now available for free by int_19h on Tuesday October 15, 2024 @02:20PM (#41851481)
You can handcode vtables in C, just as you can handcode loops in assembly (i.e. it works but it's verbose, not particularly readable, and brings more footguns).

But why would you do that if you have an instrument that lets you work at the same level as C, but with methods provided as a proper abstraction that maps exactly to what you'd have written yourself anyway?

Re:The C23 edition of Modern C is now available for free by uecker on Tuesday October 15, 2024 @03:37PM (#41852301)
I don't know, I never found the "proper abstraction" be more than irrelevant syntactic sugar. And the cost of C++ is that you end up putting everything in the header (IMHO the biggest design flaw of the language) and then compile time start to get long....
Re:The C23 edition of Modern C is now available for free by int_19h on Friday October 18, 2024 @11:26AM (#41880301)
At the very least, the proper abstraction is the one that guarantees that you pass the same pointer as the first argument of the method call as the one you used to read the vtable from.

And no, you don't have to put everything in the header with C++, and especially not if you're using it in "C with classes" mode. C++ only needs it for templates - and even then only if you want to use implicit instantiation, with `extern template` available for fine-grained control allowing you to have those specializations separately compiled and implementations kept out of the header file.

Re:The C23 edition of Modern C is now available for free by staunton on Tuesday October 15, 2024 @02:06PM (#41851286)
On a high level, "object orientation" means you think of your code as representing the state and interactions of objects. You can equally well do this in assembly. If you think of some namespace as a "singleton object" then that's what it is.

I guess what you're really asking is what are the best or most common ways to do OO in C?

Re:The C23 edition of Modern C is now available for free by f1shy on Tuesday October 15, 2024 @02:14PM (#41851403)
Oh. I learned that object orientation is primarily a way to structure data and code, such that the data is encapsulated with the code that works on it, in so called objects. So an Object is the Data, plus the functions that work on the data, an ensure that some invariants are kept. In OO parlance, that code gets executed by sending messages (calling methods).

Where can I find something about objects being "think of your code as representing the state and interactions of objects" honesty totally new to me.

So no, certainly I'm not asking ways to do OO in C. But it seems to be more definitions of object orientation as I thought...

Re:The C23 edition of Modern C is now available for free by epcoa on Tuesday October 15, 2024 @02:37PM (#41851677)
> Where can I find something about objects being "think of your code as representing the state and interactions of objects" honesty totally new to me.

I’m scratching my head how you think this is materially different than what you described in your first para. s/state/data and s/interactions/methods.

If anything though I would say the GP is more aligned with the classic definition as it highlights the focus is more on the messages (interactions) themselves rather than the implementation.

Re:The C23 edition of Modern C is now available for free by int_19h on Tuesday October 15, 2024 @02:24PM (#41851523)
There's no clear definition of what OO is, so the best you can do pragmatically is look at mainstream languages that are broadly recognized as OO and try to deduce the commonalities.

If you do that, you'll notice that, for example, encapsulation is not a part of that de facto definition, because languages like Python and (until recently) JavaScript lack it, despite being considered OO.

Indeed, the only two things that appear to be consistently present in all OO languages are: 1) some notion of object identity as distinct from object state, and 2) runtime polymorphic dispatch.

Re:The C23 edition of Modern C is now available for free by adamrezich on Tuesday October 15, 2024 @02:10PM (#41851339)
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.

Re:The C23 edition of Modern C is now available for free by uecker on Tuesday October 15, 2024 @03:39PM (#41852319)
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.
Re:The C23 edition of Modern C is now available for free by adamrezich on Tuesday October 15, 2024 @05:25PM (#41853276)
In C++ where you have methods? Sure. It would be nice to have in C, though. But, alas, ABI compatibility.
Re:The C23 edition of Modern C is now available for free by listeria on Wednesday October 16, 2024 @09:11AM (#41858696)
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...