Most active commenters
  • adamrezich(3)
  • f1shy(3)

←back to thread

The C23 edition of Modern C

(gustedt.wordpress.com)
397 points bwidlar | 11 comments | | HN request time: 0.001s | source | bottom
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(6): >>41850960 #>>41851047 #>>41851166 #>>41851693 #>>41853183 #>>41855660 #
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 #
1. adamrezich ◴[] No.41851244[source]
Namespaces, methods.
replies(1): >>41851261 #
2. 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 #
3. staunton ◴[] No.41851286[source]
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?

replies(1): >>41851403 #
4. 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 #
5. f1shy ◴[] No.41851403{3}[source]
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...

replies(2): >>41851523 #>>41851677 #
6. int_19h ◴[] No.41851481[source]
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?

replies(1): >>41852301 #
7. int_19h ◴[] No.41851523{4}[source]
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.

8. epcoa ◴[] No.41851677{4}[source]
> 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.

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